canvas

渐变:

  • createLinearGradient(x1, y1, x2, y2) 起点x1, y1, 终点x2, y2
  • createRadialGradient(x1, y1, r1, x2, y2, r2) 以x1, y1为原点,半径为r1的圆,和以x2, y2为原点,半径为r2的圆
  • gradient.addColorStop(poristion, color) position是一个0.0-1.0之间的数值,表示渐变中颜色所在的相对位置,color表示颜色
///////////////////////双层渲染,透明层放在不透明层上层/////////线性渐变
        var canvas = document.querySelector('canvas');
        if (canvas.getContext) {
            var ctx = canvas.getContext('2d');

            var lineargradient1 = ctx.createLinearGradient(0, 300, 0, 0);
            lineargradient1.addColorStop(0, 'rgba(0, 0, 0, 1)');
            lineargradient1.addColorStop(1, 'rgba(0, 0, 0, 0)');

            ctx.clearRect(0, 0, 300, 300);

            var lineargradient2 = ctx.createLinearGradient(300, 0, 0, 0);
            lineargradient2.addColorStop(0, 'rgba(255, 0, 0, 1)');
            lineargradient2.addColorStop(1, 'rgba(255, 255, 255, 1');

            ctx.fillStyle = lineargradient2;
            ctx.fillRect(0, 0, 300, 300);
            ctx.fillStyle = lineargradient1;
            ctx.fillRect(0, 0, 300, 300);

        }
///////////////////////////径向渐变
            var radialgradient = ctx.createRadialGradient(0,150,50,0,140,120);
            radialgradient.addColorStop(0, 'red');
            radialgradient.addColorStop(0.9, 'yellow');
            radialgradient.addColorStop(1, 'rgba(0, 255, 0, 0)');

            var radialgradient2 = ctx.createRadialGradient(45,45,10,52,50,30);
            radialgradient2.addColorStop(0, 'red');
            radialgradient2.addColorStop(0.9, 'yellow');
            radialgradient2.addColorStop(1, 'rgba(0, 255, 0, 0)');

            ctx.fillStyle = radialgradient;
            ctx.fillRect(0, 0, 150, 150);
            ctx.fillStyle = radialgradient2;
            ctx.fillRect(0, 0, 150, 150);

描边

ctx.strokeStyle = color;
ctx.lineWidth = ; ////设置线条宽度
ctx.strokeRect(x, y, width, height)

填充

ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);

画圆

ctx.beginPath()
ctx.arc(x, y, r, startrad, endrad);
ctx.fill() / ctx.stroke()
ctx.closePath()
*****ctx.arcTo(cpX, cpY, endX, endY)******

fill()会将路径填充城一个图形 stroke()不会

绘制虚线

ctx.beginPath();
ctx.setLineDash([40,30,20,10]);
ctx.strokeStyle = 'blue';
ctx.moveTo(10, 120);
ctx.lineTo(400, 120);
ctx.stroke();

贝塞尔函数

quadraticCurveTo(cpX, cpY, endX, endY) 依据点 结束点

公式

弧长 = 半径 * 圆心角
1° = π / 180°
度数 * π / 180° = 弧长

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 使用样式和颜色 色彩 colors 如果想要给图形上色,有两个重要的属性可以做到:fillStyle 和 stro...
    闲不住的李先森阅读 1,767评论 0赞 0
  • 渐变 Gradients 可以用线性或者径向的渐变来填充或描边赋给图形的 fillStyle 或 strokeSt...
    LemonnYan阅读 9,713评论 0赞 0
  • HTML5 中新增的一个相当于画布的标签,自带各种 API ,通过 JS 调用和搭配,实现图案绘制和想要的效果。 ...
    Primers阅读 1,650评论 0赞 1
  • 一、什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像 画布是...
    EndEvent阅读 805评论 0赞 1
  • 一.Canvas的基本介绍 1.什么是Canvas 定义:是HTML5提供的一种新标签, ie9才开始支持的,Ca...
    LiLi原上草阅读 853评论 0赞 3

友情链接更多精彩内容