码迷,mamicode.com
首页 > 其他好文 > 详细

canvas给图形添加颜色

时间:2019-02-09 11:51:34      阅读:695      评论:0      收藏:0      [点我收藏+]

标签:com   http   透明度   code   alt   const   png   path   透明图   

canvas给图形添加颜色

  1. 合法属性

    ctx.fillStyle = ‘orange‘;
    ctx.fillStyle = ‘#FFA500‘;
    ctx.fillStyle = ‘rgb(255, 165, 0)‘;
    ctx.fillStyle = ‘rgba(255, 165, 0, 1)‘;
    
    strokeStyle类似
    
    ctx.globalAlpha = 0.2; 设置全局透明度
  2. 给图形上色

    技术图片

    const canvas = document.getElementById(‘canvas‘);
    const ctx = canvas.getContext(‘2d‘);
    
    ctx.fillStyle=‘orange‘;
    ctx.fillRect(25, 25, 100, 100);
    
    ctx.fillStyle=‘blue‘;
    ctx.fillRect(125, 25, 100, 100);
    
    ctx.fillStyle=‘green‘;
    ctx.fillRect(25, 125, 100, 100);
    
    ctx.fillStyle=‘red‘;
    ctx.fillRect(125, 125, 100, 100);
  3. 给图形边框上色

    技术图片

    ctx.strokeStyle=‘rgb(255, 165, 0)‘;
    ctx.beginPath();
    ctx.arc(200, 200, 50, 0, (Math.PI/180)*360, true);
    ctx.stroke();
  4. 绘制半透明图形

    技术图片

    ctx.fillStyle=‘rgb(255, 165, 0)‘;
    ctx.beginPath();
    ctx.arc(200, 200, 50, 0, (Math.PI/180)*360, true);
    ctx.fill();
    
    ctx.globalAlpha = 0.2;
    
    ctx.fillStyle=‘white‘
    ctx.beginPath();
    ctx.arc(200, 200, 10, 0, Math.PI * 2, true);
    ctx.fill();
    
    ctx.beginPath();
    ctx.arc(200, 200, 20, 0, Math.PI * 2, true);
    ctx.fill();
    
    ctx.beginPath();
    ctx.arc(200, 200, 30, 0, Math.PI * 2, true);
    ctx.fill();

canvas给图形添加颜色

标签:com   http   透明度   code   alt   const   png   path   透明图   

原文地址:https://www.cnblogs.com/ye-hcj/p/10357241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!