typescript重写canvas --7.利用clip在指定区域绘图
1.使用 canvas 利用clip在指定区域绘图
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<canvas id="myCanvas" width="250" height="200">
你的浏览器不支持HTML5
</canvas>
<script type="text/javascript">
var canvas=document.getElementById("myCanvas");
var context=canvas.getContext("2d");
context.arc(100,100,40,0,360*Math.PI/180,true);
context.clip();
context.beginPath();
//设定颜色
context.fillStyle="lightblue";
//绘制矩形
context.fillRect(0,0,300,150);
</script>
</body>
</html>
显示效果如下
2.typescript重写canvas --利用clip在指定区域绘图
html文件
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="400" height="400">
你的浏览器不支持HTML5
</canvas>
<script src="./js/7.js"></script>
</body>
</html>
ts文件
const canvas=<HTMLCanvasElement>document.getElementById("myCanvas")
const context=canvas.getContext("2d")
if (context){
context.arc(100,100,40,0,360*Math.PI/180,true);
context.clip();
context.beginPath();
//设定颜色
context.fillStyle="lightblue";
//绘制矩形
context.fillRect(0,0,300,150);
}
生成的js文件
"use strict";
const canvas = document.getElementById("myCanvas");
const context = canvas.getContext("2d");
if (context) {
context.arc(100,100,40,0,360*Math.PI/180,true);
context.clip();
context.beginPath();
//设定颜色
context.fillStyle="lightblue";
//绘制矩形
context.fillRect(0,0,300,150);
}
标签:canvas,typescript,const,clip,绘图,context,100
From: https://blog.51cto.com/u_14483476/6239996