H 5新しいラベルcanvasキャンバス
5573 ワード
canvasキャンバス
ラベルラベル:webフロントエンド
直接コードをくどくど言う
注意:canvasの幅はラベルでしか設定できません.cssで設定しないでください.設定できないのではなく、設定した値はあなたが設定したものではありません.まず簡単な三角形 を書きますはctxを充填する.fill();
塗りつぶし色を設定:ctx.fillStyle = 'red';描画矩形:ctx.storkeRect(x,y,w,h); 塗りつぶし矩形を描く:ctx.fillRect(x,y,w,h); フィレット を設定する.描画フォント 円を描く 二次ベッセル曲線と三次ベッセル曲線を描く 画画像img キャンバス をクリア
キャンバスのクリアは一般的にアニメーションで使用されます座標系移動 座標系回転
次はcanvasで書いた時計です(わかりやすく書くために少しlow)
ラベルラベル:webフロントエンド
直接コードをくどくど言う
注意:canvasの幅はラベルでしか設定できません.cssで設定しないでください.設定できないのではなく、設定した値はあなたが設定したものではありません.
var canvas = document.getElementById('canvas');
//
var ctx = canvas.getContext('2d');
//
ctx.beginPath();
//
ctx.moveTo(0,0);
//
ctx.lineTo(250,250);
ctx.lineTo(500,0);
// closePath() , , ; , closePath
ctx.closePath();
// ( )
//
ctx.strokeStyle='blue';
// ( )
ctx.lineWidth=50;
塗りつぶし色を設定:ctx.fillStyle = 'red';
// ( closePath)
ctx.lineCap='round';
//
ctx.lineJoin='round';
// ctx.font='50px ';
// ( )
// ctx.textAline='right';//
// ctx.textBaseline='bottom';//
// ctx.strokeText('Hello,World',100,100);
// ctx.fillText('Hello,World',100,300,100);
/*
1: x
2: y
3:
4: ,
5: ,
6: ,true ,false
Math.PI 。 Math.PI/2 90 ;
n*Math.PI/180 n
*/
ctx.arc(250,250,200,Math.PI,Math.PI/2,false);
ctx.stroke();
// ( )
// moveTo ,
// quadraticCurveTo()
// 1: x
// 2: y
// 3: x
// 4: y
ctx.moveTo(0,0);
ctx.quadraticCurveTo(250,500,500,0);
ctx.stroke();
// ( )
// 1: 1 x
// 2: 1 y
// 3: 2 x
// 4: 2 y
// 5: 1 x
// 6: 1 y
ctx.moveTo(0,0);
ctx.bezierCurveTo(500,0,0,500,500,500);
ctx.stroke();
// canvas , image
var img=new Image();
img.src='images/1.jpg';
img.onload=function (){
// ,
/*
: 1:img;
2:x;
3:y;
4: width;
5: height;
6: x;
7: y;
8: width;
9: height;
*/
// ctx.drawImage(img,10,10);
// ctx.drawImage(img,10,10,200,200);
ctx.drawImage(img,10,10,200,200,100,100,200,200);
}
キャンバスのクリアは一般的にアニメーションで使用されます
//
// :x,y,w,h
// ctx.clearRect(10,10,200,200);
ctx.translate(x,y);
ctx.rotate(Math.PI/6);
次はcanvasで書いた時計です(わかりやすく書くために少しlow)
<style>
body{
background: #000;
}
canvas{
background: #fff;
}
</style>
<canvas id="cc" width="400px" height="400px"/>
<script>
var cc = document.getElementById('cc');
var ctx = cc.getContext('2d');
function time(){
var x = 200;
var y = 200;
var r = 150;
var oDate = new Date();
var hours = oDate.getHours();
var minutes = oDate.getMinutes();
var seconds = oDate.getSeconds();
var hValue = (-90 + hours * 30 + minutes/2)*Math.PI/180;
var mValue = (-90 + minutes * 6)*Math.PI/180;
var sValue = (-90 + seconds * 6)*Math.PI/180;
ctx.beginPath();
for(var i=0; i<60 ; i++){
ctx.moveTo(x,y);
ctx.arc(x,y,r,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false);
}
ctx.closePath();
ctx.stroke();
//
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x,y,r*18/20,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
//
ctx.lineWidth = 3;
ctx.beginPath();
for(var i=0; i<12 ; i++){
ctx.moveTo(x,y);
ctx.arc(x,y,r,30*i*Math.PI/180,30*(i+1)*Math.PI/180,false);
}
ctx.closePath();
ctx.stroke();
//
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x,y,r*16/20,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
//
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x,y,r*8/20,hValue,hValue,false);
ctx.closePath();
ctx.stroke();
//
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x,y,r*14/20,mValue,mValue,false);
ctx.closePath();
ctx.stroke();
//
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x,y,r*16/20,sValue,sValue,false);
ctx.closePath();
ctx.stroke();
}
setInterval(time,1000);
</script>
</code></pre>
</article>
</div>
</div>