キャンバス
HTML5キャンバスは、私のお気に入りのラベル内のすべての新機能の私のお気に入りの一つです。それはあまりにも強かった、とあるので、興味深い様々な効果を達成することができます。
実質的方法を用いて、1キャンバス
- 它是一个行内块元素
- 默认大小是 300 x 150,不能在 css 里给他设置样式,只能在标签内写它的属性。如 width = 400,height = 300
- 获取画布
var canvas = document。querySelector("canvas")
- 获取画笔(上下文)
var ctx = canvas.getContext('2d')
2.キャンバスは、基本的なグラフィックスを描画します
填充矩形
ctx.fillRect(0,0,100,100)
fill:跟填充有关
Rect: 描绘一个矩形
填充图形设置样式
ctx.fillStyle = 'green'
描边矩形
ctx.strokeRect(100,100,100,100)
描边图形设置样式
ctx.strokeStyle = 'white'
ctx.lineWidth = 100
清除整个画布
ctx.clearRect(0,0,canvas.width,canvas.height)
画线段
ctx.moveTo(100,100)
ctx.lineTo(100,100)
描边
ctx.stroke()
填充
ctx.fill()-
起始点和结束点连接
ctx.closePath()
ctx.save()开头
......
ctx.restore()结尾
3.キャンバス時計
使用して、我々は規模や時間などの時計を、描くことができ、スケールの毎秒散歩は常にデータオブジェクトにタイマーによって更新することができますキャンバス。
var canvas = document.querySelector("canvas");
var ctx = canvas.getContext("2d");
function move() {
ctx.save()
ctx.translate(300,300)
// 初始化一些公共的样式
ctx.lineCap = 'round'
ctx.strokeStyle = 'black'
ctx.lineWidth = 8
ctx.scale(0.5,0.5)
// 画外面的圆
ctx.save();
ctx.beginPath();
ctx.strokeStyle = 'gold';
ctx.arc(0,0,150,0,2*Math.PI);
ctx.stroke();
ctx.restore();
// 画里面的刻度
ctx.save()
ctx.beginPath();
for (var i=0; i < 12; i++) {
ctx.moveTo(0,-125);
ctx.lineTo(0,-140);
ctx.stroke()
ctx.rotate(30*Math.PI/180)
}
ctx.restore()
// 分针刻度
ctx.save()
ctx.lineWidth = 3
for (var i = 0; i < 60; i++) {
if (i % 5 != 0){
ctx.beginPath()
ctx.moveTo(0,-135);
ctx.lineTo(0,-140);
ctx.stroke()
}
ctx.rotate(6*Math.PI/180)
}
ctx.restore()
// 当前时间
var date = new Date()
var s = date.getSeconds()
var min = date.getMinutes() + s/60
var h = date.getHours() + min/60
// 时针
ctx.save()
ctx.rotate(30*h*Math.PI/180)
ctx.lineWidth = 14
ctx.beginPath()
ctx.moveTo(0,-80)
ctx.lineTo(0,20)
ctx.stroke()
ctx.restore()
// 分针
ctx.save()
ctx.lineWidth = 10
ctx.rotate(6*min*Math.PI/180)
ctx.beginPath()
ctx.rotate(-30*Math.PI/180)
ctx.moveTo(0,-120)
ctx.lineTo(0,30)
ctx.stroke()
ctx.restore()
//秒针
ctx.save()
ctx.lineWidth = 6
ctx.strokeStyle = 'pink'
ctx.fillStyle = 'pink'
ctx.rotate(6*s*Math.PI/180)
ctx.beginPath()
ctx.arc(0,0,10,0,2*Math.PI)
ctx.fill()
ctx.beginPath()
ctx.moveTo(0,-125)
ctx.lineTo(0,30)
ctx.stroke()
ctx.beginPath()
ctx.arc(0,-135,10,0,2*Math.PI)
ctx.stroke()
ctx.restore()
ctx.restore()
}
setInterval(function () {
ctx.clearRect(0,0,canvas.width,canvas.height)
move()
},1000)
図のように静止画像。
スクラッチ効果
使用キャンバスグラフィック合成プロパティは、合成画像の効果を達成することができます。具体的にスクラッチに適用されます。
globalCompositeOperationプロパティセットまたはリターン(新しい)イメージが画像上(既存の)ターゲットにレンダリングされたソースどのように
元画像のは=あなたがキャンバスの上で図面を配置しようとする
物体像=あなたはキャンバスの図面上の場所に持っています
var canvas = document.querySelector("canvas")
var ctx = getCtx()
log(ctx)
ctx.fillStyle = 'yellow'
ctx.fillRect(0,0,400,400)
ctx.globalCompositeOperation = 'destination-out';
// 鼠标按下
canvas.onmousedown = function (event) {
ctx.beginPath()
ctx.arc(event.clientX - canvas.offsetLeft,event.clientY - canvas.offsetTop,
20,0,2*Math.PI)
ctx.fill()
// 鼠标移动
document.onmousemove = function (event) {
ctx.beginPath()
ctx.arc(event.clientX - canvas.offsetLeft,event.clientY - canvas.offsetTop,
20,0,2*Math.PI)
ctx.fill()
}
// 鼠标抬起
document.onmouseup = function () {
document.onmousemove = document.onmouseup = null
}
return false
}