HTML Canvas
目录
Canvas
绘制一个画布:
<canvas id="Canvas1" width="200" height="100"
style="border:1px solid #000000;">在画布中创建线条:
<script>
var canvas = document.getElementById("Canvas1");
var ctx = canvas.getContext("2d");
ctx.moveTo(0,0);
//定义直线的起点
ctx.lineTo(200,100);
//定义直线的终点
ctx.stroke();
//绘制直线
ctx.beginPath();
//开始一个路径
ctx.arc(95,50,50,0,2*Math.PI);
/*arc(x,y,r,startangle,endangle)
创建弧/曲线。使用arc()创建圆:
将起始角度设置为0,将结束角度设置为2 * Math.PI。
x和y参数定义圆心的x坐标和y坐标。r参数定义圆的半径。*/
ctx.stroke();
//绘制圆
ctx.beginPath();
ctx.arc(150,50,50,0,2*Math.PI);
ctx.stroke();
</script>
文本:
<script>
var canvas = document.getElementById("Canvas2");
var ctx = canvas.getContext("2d");
ctx.font = "50px Arial";
//定义文本字体属性
ctx.fillStyle = "#50a006";
//文本颜色
ctx.textAlign = "center";
//文本对齐方式
ctx.fillText("你好", canvas.width/2, canvas.height/2);
//fillText(text,x,y) - 在画布上绘制“填充”文本
ctx.strokeText("Hello",55,70);
//strokeText(text,x,y)在画布上绘制文本
</script>
图像:
<canvas id="Canvas3" width="100" height="100"
style="border:1px solid #000000;">
<img id="scream" width="90" height="90"
src="/images/loge.jpg" alt="The Scream">
<script>
window.onload = function() {
var canvas = document.getElementById("Canvas3");
var ctx = canvas.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 0, 0);
};
</script>
渐变色卡:
<canvas id="Canvas4" width="350" height="50"
style="border:1px solid #000000;">
<script>
var c=document.getElementById("Canvas4");
var ctx=c.getContext("2d");
var grd=ctx.createLinearGradient(0,0,350,0);
createLinearGradient(x,y,x1,y1)创建一个线性渐变
grd.addColorStop(0,"red");
//中心颜色
grd.addColorStop(1,"white");
//边缘颜色
ctx.fillStyle=grd;
ctx.fillRect(0,0,350,50);
//色卡区域
var grd=ctx.createRadialGradient(25,25,5,25,25,25);
createRadialGradient(x,y,r,x1,y1,r1)创建一个径向/圆形渐变
grd.addColorStop(0,"blue");
grd.addColorStop(1,"white");
ctx.fillStyle = grd;
ctx.fillRect(0,0,50,50);
</script>
系列:notes
该系列自动来自分类: notes
- Debian创建新用户和设置防火墙
- mac在Debian安装wireguard和使用
- 再也不买不能解bl的手机了
- Firefox设置
- debian安装FFmpeg来合并youtube音频
- css 扩散列表
- 两个练习
- css RWD
- css 图片
- css 提示
- css 下拉
- css 表单
- css 导航栏
- css 单词
- HTML SVG
- HTML Canvas (当前)
- HTML input
- HTML 结构
- 电气施工图说明
- china uses dropbox
- Nginx installs SSL certificates
- debian install shadowsocks
- Visual studio code set the python environment
- install hexo