zoukankan      html  css  js  c++  java
  • Canvas入门05-渐变颜色

    线性渐变API:

    ctx.createLinearGradient(double x1, double y1, double x2, double y2) 创建一个渐变实例
    (x1, y1) 渐变的起始点
    (x2, y2) 渐变的终止点
    gradient.addColorStop(offset: double, color: string) 向渐变色中增加颜色停止点

    offset是介于0~1.0之间的double值,代表颜色停止点在渐变色上的位置;color 是CSS3 颜色字符串。

    var canvas = document.getElementById('canvas'),
        context = canvas.getContext('2d'),
        gradient = context.createLinearGradient(0, 0, canvas.width, 0);
    // 线性渐变
    gradient.addColorStop(0, 'blue');
    gradient.addColorStop(0.25, 'white');
    gradient.addColorStop(0.5, 'purple');
    gradient.addColorStop(0.75, 'red');
    gradient.addColorStop(1, 'yellow');
    
    context.fillStyle = gradient;
    context.fillRect(0, 0, canvas.width, canvas.height);

    显示效果:

    放射渐变API:

    ctx.createRadialGradient(x1: number, x2: number, r1: number, x2: number, y2: number, r2: number) 创建一个放射渐变实例

    利用两圆分离的原理,可形成放射性扇形。

    x1:起始圆的x坐标

    x2:起始圆的y坐标

    r1:起始圆的半径

    x2:终止圆的x坐标

    y2:终止圆的y坐标

    r2:终止圆的半径

    gradient.addColorStop(offset: double, color: string) 向渐变色中增加颜色停止点

    同线性渐变。

    var canvas = document.getElementById('canvas'),
        context = canvas.getContext('2d'),
        gradient = context.createRadialGradient(canvas.width/2, canvas.height, 10, canvas.width/2, 0, 100);
    
    gradient.addColorStop(0, 'blue');
    gradient.addColorStop(0.25, 'white');
    gradient.addColorStop(0.5, 'purple');
    gradient.addColorStop(0.75, 'red');
    gradient.addColorStop(1, 'yellow');
    
    context.fillStyle = gradient;
    context.fillRect(0, 0, canvas.width, canvas.height);

    效果:

  • 相关阅读:
    阿里云通过465端口发送邮件绕过25端口
    阿里云子账号Policy授权规则明细
    蓝鲸cmdb平台架构
    库文件缺失问题修复
    centos7.2中启动polkit服务启动失败
    Linux升级GCC
    Ubuntu18.04服务器使用netplan网络构建桥接kvm虚拟机
    常用服务部署脚本(nodejs,pyenv,go,redis,)
    anaconda安装教程(之前安装过python)
    【PHP】array_unique与array_array_flip
  • 原文地址:https://www.cnblogs.com/liulei-cherry/p/9896444.html
Copyright © 2011-2022 走看看