javascript - 我可以将从 Canvas 上下文创建的渐变应用到其他 Canvas 上下文吗?

标签 javascript html canvas

我可以将从 Canvas 上下文创建的渐变应用于其他 Canvas 上下文,如下面的代码吗?

var canvasContextA = document.getElementById("canvasA").getContext("2d");
var canvasContextB = document.getElementById("canvasB").getContext("2d");

var gradientFromCanvasContextA = canvasContextA.createLinearGradient(0, 0, 100, 0);
gradientFromCanvasContextA.addColorStop(0, "black");
gradientFromCanvasContextA.addColorStop(1, "white");

canvasContextB.fillStyle = gradientFromCanvasContextA;

你能根据标准给我一个答案吗? 这是未定义的行为吗?

最佳答案

有趣的问题。我在 Canvas 2D Specs 中找到了这张纸条:

In the case of addColorStop() on CanvasGradient, the "computed value of the 'color' property" for the purposes of determining the computed value of the currentColor keyword is always fully opaque black (there is no associated element). [CSSCOLOR]

This is because CanvasGradient objects are canvas-neutral — a CanvasGradient object created by one canvas can be used by another, and there is therefore no way to know which is the "element in question" at the time that the color is specified.

似乎暗示您可以按照您的要求进行操作,但仅限于渐变和可能的字体属性。

关于javascript - 我可以将从 Canvas 上下文创建的渐变应用到其他 Canvas 上下文吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35096709/

相关文章:

javascript - HTML Canvas - JavaScript 动态旋转问题

javascript - FabricJS Canvas 触摸坐标

javascript - 如何计算 HTML Canvas 弧线结束 Angular 的 x 和 y 坐标?

javascript - 使用隐藏输入字段使用 li 的数据值作为表单输入

jquery - 为按钮使用 SVG 图标而不是使用 awesome 字体

javascript - HTML/Javascript - 定位不一致

c# - 在 C# 中读取 HTML 表

javascript - 如何改变js中的函数参数并且所有参数都是对象?

javascript - Meteor - 模板助手参数(空格键)

javascript - jQuery 悬停() : mouseout does not fire when using overflow:auto (with scrollbars)