当前位置:   article > 正文

小程序API能力集成指南——画布API汇总(三)_小程序 ctx.fill 和 ctx.stoke

小程序 ctx.fill 和 ctx.stoke

CanvasContext

canvas 组件的绘图上下文。

方法如下(2):

arc

CanvasContext.arc

CanvasContext.arc(number x, number y, number r, number sAngle, number eAngle, boolean counterclockwise)

功能描述

创建一条弧线。

  • 创建一个圆可以指定起始弧度为 0,终止弧度为 2 * Math.PI。
  • 用 stroke 或者 fill 方法来在 canvas 中画弧线。

参数

number x

圆心的 x 坐标

number y

圆心的 y 坐标

number r

圆的半径

number sAngle

起始弧度,单位弧度(在 3 点钟方向)

number eAngle

终止弧度

boolean counterclockwise

弧度的方向是否是逆时针

示例代码

 <canvas canvas-id="myCanvas" style="border: 1px solid;"/>
  1. const ctx = ty.createCanvasContext('myCanvas');
  2. // Draw coordinates
  3. ctx.arc(100, 75, 50, 0, 2 * Math.PI);
  4. ctx.setFillStyle('#EEEEEE');
  5. ctx.fill();
  6. ctx.beginPath();
  7. ctx.moveTo(40, 75);
  8. ctx.lineTo(160, 75);
  9. ctx.moveTo(100, 15);
  10. ctx.lineTo(100, 135);
  11. ctx.setStrokeStyle('#AAAAAA');
  12. ctx.stroke();
  13. ctx.setFontSize(12);
  14. ctx.setFillStyle('black');
  15. ctx.fillText('0', 165, 78);
  16. ctx.fillText('0.5*PI', 83, 145);
  17. ctx.fillText('1*PI', 15, 78);
  18. ctx.fillText('1.5*PI', 83, 10);
  19. // Draw points
  20. ctx.beginPath();
  21. ctx.arc(100, 75, 2, 0, 2 * Math.PI);
  22. ctx.setFillStyle('lightgreen');
  23. ctx.fill();
  24. ctx.beginPath();
  25. ctx.arc(100, 25, 2, 0, 2 * Math.PI);
  26. ctx.setFillStyle('blue');
  27. ctx.fill();
  28. ctx.beginPath();
  29. ctx.arc(150, 75, 2, 0, 2 * Math.PI);
  30. ctx.setFillStyle('red');
  31. ctx.fill();
  32. // Draw arc
  33. ctx.beginPath();
  34. ctx.arc(100, 75, 50, 0, 1.5 * Math.PI);
  35. ctx.setStrokeStyle('#333333');
  36. ctx.stroke();
  37. ctx.draw();

针对 arc(100, 75, 50, 0, 1.5 * Math.PI)的三个关键坐标如下:

  • 绿色: 圆心 (100, 75)
  • 红色: 起始弧度 (0)
  • 蓝色: 终止弧度 (1.5 * Math.PI)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/215737?site
推荐阅读
相关标签