javascript - 用一种以上的颜色按程序填充一个形状

标签 javascript canvas paperjs

我正在用节点、js 等制作一个应用程序。我想填充可以通过数据点或其他具有不同分层颜色的格式创建的自定义形状。例如,我有一个三 Angular 形。我想用红色填充底部的 1/3,用蓝色填充中间的 1/3,用绿色填充顶部的 1/3。我该怎么做?

我正在查看 Paper.js 和基本 Canvas ,但它们似乎只有单色填充。

感谢您的任何建议!

最佳答案

我知道答案已被接受,但我想为 future 的读者提供一个非常简单的方法。作为奖励,它会自动计算每个部分的高度并且速度很快,使用线性渐变 -

结果将是

snap

代码和演示

var ctx = document.querySelector("canvas").getContext("2d"),
    grad = ctx.createLinearGradient(0, 0, 0, 150);

grad.addColorStop(0, "red");     // start of red
grad.addColorStop(1/3, "red");   // end of red at 1/3

grad.addColorStop(1/3, "gold");  // start of gold at 1/3
grad.addColorStop(2/3, "gold");  // end of gold at 2/3

grad.addColorStop(2/3, "blue");  // start of blue at 2/3
grad.addColorStop(1, "blue");    // end of blue at 3/3

// Fill a triangle:
ctx.moveTo(75, 0); ctx.lineTo(150, 150); ctx.lineTo(0, 150);
ctx.fillStyle = grad;
ctx.fill();
<canvas/>

使用合成技术的动画版

var ctx = document.querySelector("canvas").getContext("2d"),
    grad = ctx.createLinearGradient(0, 0, 0, 150),
    step = grad.addColorStop.bind(grad), // function reference to simplify
    dlt = -3, y = 150;

step(0, "red");     // start of red
step(1/3, "red");   // end of red at 1/3
step(1/3, "gold");  // start of gold at 1/3
step(2/3, "gold");  // end of gold at 2/3
step(2/3, "blue");  // start of blue at 2/3
step(1, "blue");    // end of blue at 3/3

// store a triangle path - we'll reuse this for the demo loop
ctx.moveTo(75, 0); ctx.lineTo(150, 150); ctx.lineTo(0, 150);

(function loop() {
  ctx.globalCompositeOperation = "copy";  // will clear canvas with next draw

  // Fill the previously defined triangle path with any color:
  ctx.fillStyle = "#000";  // fill some solid color for performance
  ctx.fill();
  
  // draw a rectangle to clip the top using the following comp mode:
  ctx.globalCompositeOperation = "destination-in";
  ctx.fillRect(0, y, 150, 150 - y);

  // now that we have the shape we want, just replace it with the gradient:
  // to do that we use a new comp. mode
  ctx.globalCompositeOperation = "source-in";
  ctx.fillStyle = grad;
  ctx.fillRect(0, 0, 150, 150);
  
  y += dlt; if (y <= 0 || y >= 150) dlt = -dlt;  
  requestAnimationFrame(loop);
})();
<canvas/>

用于动画的缓存渐变图像(推荐)

var ctx = document.querySelector("canvas").getContext("2d"),
    tcanvas = document.createElement("canvas"),    // to cache triangle
    tctx = tcanvas.getContext("2d"),
    grad = tctx.createLinearGradient(0, 0, 0, 150),
    step = grad.addColorStop.bind(grad), // function reference to simplify
    dlt = -3, y = 150;

step(0, "red");     // start of red
step(1/3, "red");   // end of red at 1/3
step(1/3, "gold");  // start of gold at 1/3
step(2/3, "gold");  // end of gold at 2/3
step(2/3, "blue");  // start of blue at 2/3
step(1, "blue");    // end of blue at 3/3

// draw triangle to off-screen canvas once.
tctx.moveTo(75, 0); tctx.lineTo(150, 150); tctx.lineTo(0, 150);
tctx.fillStyle = grad; tctx.fill();

(function loop() {
  ctx.clearRect(0, 0, 150, 150);

  // draw clipped version of the cached triangle image
  if (150-y) ctx.drawImage(tcanvas, 0, y, 150, 150 - y, 0, y, 150, 150 - y);

  y += dlt; if (y <= 0 || y >= 150) dlt = -dlt;  
  requestAnimationFrame(loop);
})();
<canvas/>

您可以使用渐变线改变方向,这决定了渐变的 Angular 。

// vertical 
ctx.createLinearGradient(0, 0, 0, 150); // x1, y1, x2, y2

// hortizontal
ctx.createLinearGradient(0, 0, 150, 0); // x1, y1, x2, y2

// 45° degrees
ctx.createLinearGradient(0, 0, 150, 150); // x1, y1, x2, y2

等等

关于javascript - 用一种以上的颜色按程序填充一个形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29022649/

相关文章:

javascript - 如何使用 Paper.js 计算闭合多边形路径的内 Angular ?

javascript - 基本的 Javascript 数学文本字段

javascript - 检测旋转的矩形是否在 Canvas 弧内

javascript - 如何使用 javascript 拦截站点 http 请求?

javascript - 在具有存储坐标的图像上绘制时考虑 Canvas 大小差异

javascript - Paper.js onFrame 未在离屏 Canvas 上调用

javascript - 如何使用来自外部资源的 paperscript?

javascript - 使用.change javascript计算排除周末的2个日期

javascript - 将 iOS 模块从 pod 导入 nativescript 插件