CanvasContext
canvas 组件的绘图上下文。
方法如下(3):
scale
CanvasContext.scale
CanvasContext.scale(number scaleWidth, number scaleHeight)
功能描述
在调用后,之后创建的路径其横纵坐标会被缩放。多次调用倍数会相乘。
参数
number scaleWidth
横坐标缩放的倍数 (1 = 100%,0.5 = 50%,2 = 200%)
number scaleHeight
纵坐标轴缩放的倍数 (1 = 100%,0.5 = 50%,2 = 200%)
示例代码
const ctx = ty.createCanvasContext('myCanvas');
ctx.strokeRect(10, 10, 25, 15);
ctx.scale(2, 2);
ctx.strokeRect(10, 10, 25, 15);
ctx.scale(2, 2);
ctx.strokeRect(10, 10, 25, 15);
ctx.draw();
rotate
CanvasContext.rotate
CanvasContext.rotate(number rotate)
功能描述
以原点为中心顺时针旋转当前坐标轴。多次调用旋转的角度会叠加。原点可以用 translate
方法修改。
参数
number rotate
旋转角度,以弧度计 degrees * Math.PI/180;degrees 范围为 0-360
示例代码
const ctx = ty.createCanvasContext('myCanvas');
ctx.strokeRect(100, 10, 150, 100);
ctx.rotate((20 * Math.PI) / 180);
ctx.strokeRect(100, 10, 150, 100);
ctx.rotate((20 * Math.PI) / 180);
ctx.strokeRect(100, 10, 150, 100);
ctx.draw();
<