我想用 Canvas 制作一个渐变文本,我的电脑渲染代码正确,但 android 显示它就像普通渐变一样,里面有文本。如果可能的话,我该如何解决这个问题?
这是 JavaScript:
function text() {
var canvas = document.getElementById('header-text');
var ctx = canvas.getContext('2d');
var gradient = ctx.createLinearGradient(0, 0, 0, 29);
gradient.addColorStop(0, "#fcfcfc");
gradient.addColorStop(1, "#ccc");
ctx.font = "bold 29px helvetica, arial, sans-serif";
ctx.shadowColor = "#232323";
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 2;
ctx.fillStyle = gradient;
ctx.fillText("Company name", 0, 23);
}
最佳答案
很抱歉,这是一个错误。
或者更确切地说,fillText 的渐变尚未在 android 浏览器上实现。如果你这样写:
var gradient = ctx.createLinearGradient(0, 0, 0, 29);
gradient.addColorStop(0, "#fcfcfc");
gradient.addColorStop(1, "#ccc");
ctx.fillStyle = 'red';
ctx.fillStyle = gradient; // replace the fillstyle with a gradient
ctx.fillText("Testing", 0, 23);
ctx.fillRect(0,0,20,20)
您会看到文本和矩形在 Chrome 中都有渐变。
但在 android 设备上,文本将是红色的,矩形将是渐变的!
Chrome 本身仍然不完全支持规范作者(他自己是 Google 员工)提出的所有 Canvas 渐变案例。我提交了一份关于它的错误报告 here .
关于android - android问题中的HTML5 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585502/