實習期間讓我用canvas畫表情,比較簡單,話不多說直接上程式碼:
<body><div id=canvas-warp> <canvas id=canvas style=display: block; margin: 200px auto;> 你的瀏覽器居然不支援Canvas! </canvas></div><script> window. onload = function () { var canvas = document.getElementById(canvas); canvas.width = 400; canvas.height = 400; //取得上下文var context = canvas.getContext(2d); //用來畫有填滿色圓的函數參數分別為圓心座標,半徑,起始與終止位置,線顏色,填滿顏色function drawCircle(x2, y2, r2, a2, b2, lineColor, FillColor) { context.beginPath(); context.arc(x2, y2, r2, a2, b2 * Math.PI); context.strokeStyle = lineColor; context.fillStyle = FillColor; context.fill(); //確認填入context.stroke(); }; //用來畫圓弧函數預設線條為黑色無填充參數分別為:圓心x座標,圓心y座標,半徑,開始位置,終止位置function drawsArc(x, y, r, l1, l2) { context.beginPath(); context.arc(x, y, r, l1 * Math.PI, l2 * Math.PI); context.strokeStyle = black; context.stroke(); }; //用來畫眼睛的函數function darwEyes(x1, y1, a1, b1) { //參數分別為橢圓圓心位置長軸短軸context.strokeStyle = #754924 ParamEllipse(context, x1, y1, a1, b1); //橢圓function ParamEllipse(context, x, y, a, b) { //使每次循環所繪製的路徑(弧線)接近1像素var step = (a > b) ? 1 / a : 1 / b; context.beginPath(); context.moveTo(x + a, y); //從橢圓的左端點開始繪製for (var i = 0; i < 2 * Math.PI; i += step) { //參數為i,表示度數(弧度) context.lineTo(x + a * Math .cos(i), y + b * Math.sin(i)); } context.closePath(); context.fillStyle = #754924; context.fill(); context.stroke(); }; }; //臉drawCircle(200, 200, 200, 0, 2, #EEE685, #FCF200); //左眼context.strokeStyle = #754924 darwEyes(116, 130, 18, 25); , 127, 12, 0, 2, #754924, #F5F5F5); //右眼darwEyes(296, 130, 18, 25); drawCircle(290, 127, 12, 0, 2, #754924, #F5F5F5); , 50, 1.3, 1.7); //右眉毛drawsArc(300, 100, 50, 1.3, 1.7); //嘴巴drawsArc(200, 120, 180, 0.3, 0.7); }</script></body>效果圖
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。