首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >context2d.strokeText()在FF3.6/4下描边路径

context2d.strokeText()在FF3.6/4下描边路径
EN

Stack Overflow用户
提问于 2011-01-14 18:16:23
回答 2查看 492关注 0票数 0

我在FF (3.6和4beta4)下的strokeText遇到了一个奇怪的效果,我不能在Chrome或Safari下重现。如果我绘制了一个填充的路径形状(但没有描边),然后绘制了一个描边文本,则在调用strokeText()时会对路径进行描边。

下面是我的代码:

代码语言:javascript
复制
            var el = document.getElementById('canvas'),
            ctx = el.getContext('2d');

        ctx.save();
        // draw Rect
        ctx.beginPath();
        ctx.moveTo(100, 100);
        ctx.lineTo(200, 100);
        ctx.lineTo(200, 200);
        ctx.lineTo(100, 200);
        ctx.lineTo(100, 100);
        ctx.closePath();
        ctx.fillStyle = 'red';
        ctx.fill();

                    // draw Text
        ctx.save();         
        ctx.textAlign = 'start';
        ctx.fillStyle = "rgba(0,0,0,0.0)";
        ctx.strokeStyle = "blue";
        ctx.lineWidth = 2;
        ctx.font = "bold 35pt sans-serif";
        ctx.strokeText("Hello World !", 100, 280);  

        ctx.restore();

如果运行它,rect会以蓝色描边,而它不应该是蓝色的。

你是否发现这段代码有什么问题,或者是FF中的一个bug?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2011-01-14 21:34:21

这个问题已经在Firefox4beta 8中修复了(刚刚测试过...)。我认为相关的bug是:https://bugzilla.mozilla.org/show_bug.cgi?id=499628

票数 1
EN

Stack Overflow用户

发布于 2011-01-14 18:30:20

这样画长方形怎么样?

代码语言:javascript
复制
var el = document.getElementById('canvas'),
ctx = el.getContext('2d');

ctx.save();
// draw Rect
ctx.fillStyle = 'red';
ctx.fillRect  (100, 100, 100, 100); // <===
// draw Text
ctx.save();         
ctx.textAlign = 'start';
ctx.fillStyle = "rgba(0,0,0,0.0)";
ctx.strokeStyle = "blue";
ctx.lineWidth = 2;
ctx.font = "bold 35pt sans-serif";
ctx.strokeText("Hello World !", 100, 280);  

ctx.restore();

编辑下面的关于strokeText的问题,似乎如果你在绘制和填充路径之前绘制文本就可以解决它

代码语言:javascript
复制
var el = document.getElementById('canvas'),
ctx = el.getContext('2d');

// draw Text -> at first
ctx.save();         
ctx.textAlign = 'start';
ctx.fillStyle = "rgba(0,0,0,0.0)";
ctx.strokeStyle = "blue";
ctx.lineWidth = 2;
ctx.font = "bold 35pt sans-serif";
ctx.strokeText("Hello World !", 100, 280);

ctx.save();
// draw Rect
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(200, 100);
ctx.lineTo(200, 200);
ctx.lineTo(100, 200);
ctx.lineTo(100, 100);
ctx.closePath();
ctx.fillStyle = 'red';
ctx.fill();

ctx.restore();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4689994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档