简体   繁体   English

HTML5 Canvas:在调整大小时设置上下文

[英]HTML5 Canvas: Setting context on resize

I have a project using HTML5 Canvas (createjs) and I've had issues with spikes on text strokes, meaning I have to set the miterlimit of the 2d context. 我有一个使用HTML5 Canvas(createjs)的项目,但文本笔触出现尖峰问题,这意味着我必须设置2d上下文的下限。 However, the parent (which I have no control over) scales the canvas when the window is resized, which obviously resets the canvas context. 但是,父窗口(我无法控制)在调整窗口大小时缩放画布,这显然会重置画布上下文。

Now, I want to avoid putting an onresize event inside the client - my first thought is just to use the createjs Ticker thus: 现在,我想避免在客户端中放置onresize事件-我的第一个想法就是使用createjs Ticker:

createjs.Ticker.addEventListener("tick", handleTick);
function handleTick(event) {
    var ctx = canvas.getContext('2d');
    ctx.miterLimit = 2;
}

Though this seems a little wasteful, is there a more efficient way of doing this, without using DOM events? 尽管这似乎有些浪费,但是有没有使用DOM事件的更有效方法呢?

Your approach might work, but its definitely a hack, since you can't expect that context properties will be maintained, or that they won't be applied in the wrong place. 您的方法可能有效,但是绝对可以避免,因为您不能期望上下文属性会被保留,或者它们不会在错误的位置应用。

If you do want to patch the display list to update the context, you can use the "drawstart" event, which fires before the display list is drawn: 如果确实要修补显示列表以更新上下文,则可以使用“ drawstart”事件,该事件在绘制显示列表之前触发:

stage.on("drawstart", function(e) {
    var ctx = stage.canvas.getContext("2d");
    ctx.miterLimit = 2;
});

However if you want a better approach that is instance-specific, you can extend the Text class to append any context properties you want. 但是,如果您想要一种更好的特定于实例的方法,则可以扩展Text类以附加所需的任何上下文属性。 Here is a quick example where miterLimit is stored and applied any time the text is drawn. 这是一个简单的示例,其中只要绘制文本,就会存储并应用miterLimit In this example, you can create multiple instances and set different miter limits on them. 在此示例中,您可以创建多个实例并为其设置不同的斜接限制。 Note that you might want to also support other properties such as lineJoin. 请注意,您可能还希望支持其他属性,例如lineJoin。

http://jsfiddle.net/cr4hmgqp/2/ http://jsfiddle.net/cr4hmgqp/2/

(function() {
    "use strict"
  function MiterText(text, font, color, miterLimit) {
    this.Text_constructor(text,font,color);
    this.miterLimit = miterLimit;
  };
  var p = createjs.extend(MiterText, createjs.Text);
  p.draw = function(ctx, ignoreCache) {
    ctx.miterLimit = this.miterLimit;
    if (this.Text_draw(ctx, ignoreCache)) { return true; }
    return true;
  };
  p.clone = function() {
        return this._cloneProps(new MiterText(this.text, this.font, this.color, this.miterLimit));
    };
  createjs.MiterText = createjs.promote(MiterText, "Text");
}());

Note that this issue should hopefully be fixed in the next version of EaselJS. 请注意,希望在下一版本的EaselJS中解决此问题。 Here is the tracked issue: https://github.com/CreateJS/EaselJS/issues/781 这是跟踪的问题: https : //github.com/CreateJS/EaselJS/issues/781

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM