学习Canvas基础-圆柱渐变、设置底纹(createPattern)

1、圆柱渐变
//	createconicgradient()方法在给定坐标的点周围创建渐变。
//	createConicGradient(startAngle, x, y)
//	参数:
//	startAngle--开始渐变的角度,以弧度为单位。角度测量从中心上方垂直开始,顺时针移动。
//	x--梯度中心的x轴坐标。
//	y--梯度中心的y轴坐标。

事例
//  获取canvas画布
    const canvas = document.querySelector('#cont')
    // 获取画布的应用上下文
    const ctx = canvas.getContext('2d')
    //  createConicGradient(开始渐变角度,圆心X,圆心Y)
    var gradient = ctx.createConicGradient(Math.PI/2, 250, 250)
    gradient.addColorStop(0, 'blue')
    gradient.addColorStop(0.5, 'yellow')
    gradient.addColorStop(1, 'red')
    ctx.fillStyle = gradient
    ctx.arc(250,250,150, 0, Math.PI*2 )
    ctx.fill()

在这里插入图片描述

2、设置底纹
createPattern它通过 repetition 参数在指定的方向上重复元图像
ctx.createPattern(image, repetition)
image
    // 作为重复图像源的 CanvasImageSource 对象。可以是下列之一:
  	<img>,
  	<video>
    <canvas>
repetition
	// DOMString,指定如何重复图像。允许的值有:
    "repeat" (both directions),
    "repeat-x" (horizontal only),
    "repeat-y" (vertical only),
    "no-repeat" (neither).
	// 注意:如果为空字符串 ('') 或 null (但不是 undefined),repetition 将被当作"repeat"。
事例

repetition的值为repeat / null / ’ '状态,为平铺满

 //  获取canvas画布
    const canvas = document.querySelector('#cont')
    // 获取画布的应用上下文
    const ctx = canvas.getContext('2d')
    //  createConicGradient(开始渐变角度,圆心X,圆心Y)
    var img = new Image();
    img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png';
    img.onload = function () {
      // createPattern()
      var pattern = ctx.createPattern(img, 'repeat');
      ctx.fillStyle = pattern;
      ctx.fillRect(0, 0, 500, 500);
    }; 

在这里插入图片描述
repetition的值为repeat-x状态,为沿着X轴平铺

 //  获取canvas画布
    const canvas = document.querySelector('#cont')
    // 获取画布的应用上下文
    const ctx = canvas.getContext('2d')
    //  createConicGradient(开始渐变角度,圆心X,圆心Y)
    var img = new Image();
    img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png';
    img.onload = function () {
      // createPattern()
      var pattern = ctx.createPattern(img, 'repeat-x');
      ctx.fillStyle = pattern;
      ctx.fillRect(0, 0, 500, 500);
    }; 

在这里插入图片描述
repetition的值为repeat-x状态,为沿着y轴平铺

 //  获取canvas画布
    const canvas = document.querySelector('#cont')
    // 获取画布的应用上下文
    const ctx = canvas.getContext('2d')
    //  createConicGradient(开始渐变角度,圆心X,圆心Y)
    var img = new Image();
    img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png';
    img.onload = function () {
      // createPattern()
      var pattern = ctx.createPattern(img, 'repeat-y');
      ctx.fillStyle = pattern;
      ctx.fillRect(0, 0, 500, 500);
    }; 

在这里插入图片描述
repetition的值为no-repeat状态

 //  获取canvas画布
    const canvas = document.querySelector('#cont')
    // 获取画布的应用上下文
    const ctx = canvas.getContext('2d')
    //  createConicGradient(开始渐变角度,圆心X,圆心Y)
    var img = new Image();
    img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png';
    img.onload = function () {
      // createPattern()
      var pattern = ctx.createPattern(img, 'no-repeat');
      ctx.fillStyle = pattern;
      ctx.fillRect(0, 0, 500, 500);
    }; 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值