canvas繪製笑臉表情
分享一段程式碼例項,它實現了利用canvas繪製QQ笑臉的功能。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas id="mycanvas" width="640" height="500"></canvas> <script> var it = document.getElementById("mycanvas"); var myCanvas = it.getContext("2d"); myCanvas.arc(320, 225, 200, 0, 2 * Math.PI); var myPen = myCanvas.createRadialGradient(320, 225, 140, 320, 225, 200); myPen.addColorStop(0, "#f0e720"); myPen.addColorStop(1, "#ffca3c"); myCanvas.fillStyle = myPen; myCanvas.fill(); //嘴 myCanvas.beginPath(); myCanvas.arc(320, 225, 140, 0, 2 * Math.PI); myCanvas.fillStyle = "#7f2e00"; myCanvas.fill(); myCanvas.beginPath(); myCanvas.arc(320, 215, 140, 0, 2 * Math.PI); myCanvas.fillStyle = "#f0e720"; myCanvas.fill(); //左邊眉毛 myCanvas.beginPath(); myCanvas.moveTo(162, 78); myCanvas.quadraticCurveTo(189, 22, 235, 95); myCanvas.quadraticCurveTo(194, 44, 162, 78); myCanvas.fillStyle = "#000"; myCanvas.fill(); myCanvas.stroke(); myCanvas.closePath(); //右邊眉毛 myCanvas.beginPath(); myCanvas.moveTo(472, 78); myCanvas.quadraticCurveTo(445, 22, 399, 95); myCanvas.quadraticCurveTo(448, 44, 472, 78); myCanvas.fill(); myCanvas.stroke(); myCanvas.closePath(); //左眼 myCanvas.lineWidth = 5; myCanvas.strokeStyle = "#75291c"; myCanvas.beginPath(); myCanvas.moveTo(136, 127); myCanvas.bezierCurveTo(176, 100, 226, 100, 266, 127); myCanvas.bezierCurveTo(272, 134, 268, 146, 256, 147); myCanvas.bezierCurveTo(226, 124, 176, 124, 140, 147); myCanvas.bezierCurveTo(126, 142, 126, 134, 136, 127); myCanvas.stroke(); myCanvas.fillStyle = "#fff"; myCanvas.fill(); myCanvas.beginPath(); myCanvas.fillStyle = "#333"; myCanvas.arc(156, 128, 16, 0, 2 * Math.PI, true); myCanvas.fill(); myCanvas.closePath(); //右眼 myCanvas.beginPath(); myCanvas.moveTo(372, 127); myCanvas.bezierCurveTo(412, 100, 462, 100, 502, 127); myCanvas.bezierCurveTo(508, 134, 504, 146, 492, 147); myCanvas.bezierCurveTo(462, 124, 412, 124, 376, 147); myCanvas.bezierCurveTo(362, 142, 362, 134, 372, 127); myCanvas.stroke(); myCanvas.fillStyle = "#fff"; myCanvas.fill(); myCanvas.beginPath(); myCanvas.fillStyle = "#333"; myCanvas.arc(392, 128, 16, 0, 2 * Math.PI, true); myCanvas.fill(); myCanvas.closePath(); //臉頰 myCanvas.beginPath(); myCanvas.strokeStyle = "#fd9100"; myCanvas.fillStyle = "#fd9100"; EllipseTwo(myCanvas, 200, 160, 26, 12); myCanvas.fill(); myCanvas.stroke(); myCanvas.beginPath(); myCanvas.strokeStyle = "#fd9100"; myCanvas.fillStyle = "#fd9100"; EllipseTwo(myCanvas, 436, 160, 26, 12); myCanvas.fill(); myCanvas.stroke(); function EllipseTwo(context, x, y, a, b) { context.save(); var r = (a > b) ? a : b; var ratioX = a / r; var ratioY = b / r; context.scale(ratioX, ratioY); context.beginPath(); context.arc(x / ratioX, y / ratioY, r, 0, 2 * Math.PI, false); context.closePath(); context.restore(); context.stroke(); } </script> </body> </html>
相關文章
- canvas繪製笑臉程式碼例項Canvas
- canvas繪製卡通人臉形象效果Canvas
- canvas卡通形象笑臉效果Canvas
- jQuery和CSS3繪製動態笑臉jQueryCSSS3
- canvas 繪製矩形Canvas
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- canvas 繪製線條Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas繪製直線Canvas
- Canvas 繪製雷達圖Canvas
- canvas繪製動畫的技巧Canvas動畫
- canvas 繪製立體圓環Canvas
- canvas 繪製文字詳解Canvas
- canvas 繪製圓角矩形Canvas
- canvas 繪製矩形缺角Canvas
- canvas 繪製雙線技巧Canvas
- canvas繪製風車效果Canvas
- canvas繪製太陽系Canvas
- canvas繪製矩形框Canvas
- Canvas 繪製風向動畫Canvas動畫
- SwiftKey:全球使用者表情使用大調查 笑臉最受喜愛Swift
- canvas 繪製圖案是重疊繪製而不是重置Canvas
- canvas繪製圖案是重疊繪製而不是重置Canvas
- Canvas(3)---繪製餅狀圖Canvas
- canvas系列教程之繪製矩形Canvas
- 用canvas繪製流星夜空Canvas
- 使用canvas繪製圓弧動畫Canvas動畫
- canvas繪製不重合的圓Canvas
- canvas繪製sin正弦曲線Canvas
- 笑臉漏洞復現
- 流水賬系列之Canvas繪製-02Canvas
- canvas繪製扇形程式碼例項Canvas
- canvas lineWidth繪製原理Canvas
- canvas lineWidth 繪製原理Canvas
- canvas繪製圖片drawImage學習Canvas
- canvas繪製星星程式碼例項Canvas