锦方的个人网页 · 如果有一天你突然想起了我


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>

The Scream

渐变色卡:

<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>

系列:note

该系列自动来自分类: note

  1. 备份dokuwiki数据
  2. grep 命令
  3. zola的Front Matter完整字段的意思
  4. 从dokuwiki切换到quartz
  5. 安装immich记录
  6. 修改dokuwiki时区
  7. macos安装dokuwiki
  8. Debian创建新用户和设置防火墙
  9. mac在Debian安装wireguard和使用
  10. 再也不买不能解bl的手机了
  11. Firefox设置
  12. debian安装FFmpeg来合并youtube音频
  13. css 扩散列表
  14. 两个练习
  15. css RWD
  16. css 图片
  17. css 提示
  18. css 下拉
  19. css 表单
  20. css 导航栏
  21. css 单词
  22. HTML SVG
  23. HTML Canvas (当前)
  24. HTML input
  25. HTML 结构
  26. 电气施工图说明
  27. china uses dropbox
  28. Nginx installs SSL certificates
  29. debian install shadowsocks
  30. Visual studio code set the python environment
  31. install hexo

下一篇推荐

系列继续阅读

HTML input