canvas学习笔记

canvas基础知识

MDN的教程的学习笔记。

1 定义

利用 <canvas> 元素,通过脚本来完成图像的绘制

2 用法

2.1 绘制

创建画布

<canvas id="myCanvas" width="200" height="100"></canvas>

2.2 使用 JavaScript 来绘制图像

找到 <canvas> 元素:

var c=document.getElementById("myCanvas");

2.3 创建 context 对象

var ctx=c.getContext("2d");

//建议检测一下兼容性
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
// drawing code here
} else {
// canvas-unsupported code here
}

2.4 进行绘制

3 坐标系

坐标起点(0,0)左上角,x轴从左向右 →,y轴从上向下 ↓

4 常用工具

通过 ctx 的方法实现,ctx.method(params)

不同于 SVG,canvas 只提供两种原始工具,rectangles 和 paths

4.1 形状

描述方法参数
画一个填充方形fillRect(x, y, width, height)
= rect(x, y, width, height) + fill()
x 左上角x坐标
y 左上角y坐标
width 宽度
height 长度
画一个描边方形strokeRect(x, y, width, height)
= rect(x, y, width, height) + stroke()
同上↑
清空一个方形区域clearRect(x, y, width, height)同上↑

4.2 路径

描述方法参数
创建一个新路径beginPath()
路径方法Path methods……
结束一个路径[closePath()
+]
stroke()
fill()
路径方法
方法描述example
moveTo(x, y)……ctx.moveTo(110, 75);
lineTo(x, y)……ctx.lineTo(105, 25);
arc(x, y, radius, startAngle, endAngle, anticlockwise)anticlockwise 逆时针,默认false顺时针ctx.arc(75, 75, 50, 0, Math.PI * 2, true);
arcTo(x1, y1, x2, y2, radius)ctx.moveTo(200, 20);ctx.arcTo(200,130, 50,20, 40);
quadraticCurveTo(cp1x, cp1y, x, y)二次贝塞尔曲线ctx.moveTo(75, 25);ctx.quadraticCurveTo(25, 25, 25, 62.5);
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)三次贝塞尔曲线ctx.moveTo(75, 40);ctx.bezierCurveTo(75, 37, 70, 25, 50, 25);

4.3 文字

方法

fillText(text, x, y [, maxWidth]) - 填充文字

strokeText(text, x, y [, maxWidth]) - 描边文字

属性

font = value

textAlign = value

textBaseline = value

direction = value

example:

ctx.font = '48px serif';
ctx.textBaseline = 'hanging';
ctx.strokeText('Hello world', 0, 100);

4.4 图像

方法

drawImage(image, x, y) - 图像

drawImage(image, x, y, width, height) - 缩放图像

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) - 切割图像

属性

var img = new Image();
img.onload = function() {}
img.src = '';

5 常用属性

5.1 填充和描边 fill & stroke

fill 填充

fillStyle = color

stroke 描边

strokeStyle = color

transparent 透明度

globalAlpha = transparencyValue

line

lineWidth = value

lineCap = butt | round | square

lineJoin = round | bevel | miter

miterLimit = value

getLineDash()

setLineDash(segments)

lineDashOffset = value

shadows

shadowOffsetX = float

shadowOffsetY = float

shadowBlur = float

shadowColor = color

5.2 渐变 gradient

linear gradient 线性渐变.

方法

var gradient = createLinearGradient(x1, y1, x2, y2)

radial gradient 径向渐变

方法

var gradient = createRadialGradient(x1, y1, r1, x2, y2, r2)

属性

gradient.addColorStop(position, color)

用法

赋值给 fillStylestrokeStyle 使用

5.3 重复 patterns

方法

var pattern = createPattern(image, type)

属性

type: repeat | repeat-x | repeat-y | no-repeat

用法

赋值给 fillStyle 使用

5.4 变换 transform

save 保存

save()

restore 恢复

restore()

translate 移动

translate(x, y)

tip: 此处的移动指的是坐标系原点移动

rotate 旋转

rotate(angle)

tip: 此处的旋转指的是坐标系x轴旋转

scale 缩放

scale(x, y)

transform 变换

transform(a, b, c, d, e, f)

resetTransform()

5.5 剪切和合成 clip & mask

clip 剪切

clip()

用法类似 closePath

composite 合成

globalCompositeOperation = type

具体见 type

5.6 动画 animation

通过函数控制动画

setInterval(function, delay)

setTimeout(function, delay)

requestAnimationFrame(callback)

5.7 像素操纵

具体见 pixel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值