javascript - Stage not updating with window.onresize in TypeScript (0.9.1.1) -
i have renderer class draws html5 canvas , want redraw stuff when window resized. problem update() code executes once - inside initialize() - , resizing window doesn't anything. on hand onwindowresized() executes every time window resized. so, mistake?
/// <reference path="../lib/easeljs.d.ts" /> /// <reference path="../lib/tweenjs.d.ts" /> /// <reference path="../lib/soundjs.d.ts" /> /// <reference path="../lib/preloadjs.d.ts" /> class renderer { private mstage:createjs.stage; private mshape:createjs.shape; public initialize (stage:createjs.stage):void { this.mstage = stage; this.mstage.autoclear = true; this.update (); window.onresize = this.onwindowresized; } public update ():void { if (this.mstage) { this.mstage.canvas.width = window.innerwidth; this.mstage.canvas.height = window.innerheight; this.mshape = new createjs.shape(); this.mshape.graphics.beginfill("#dddddd"); this.mshape.graphics.drawrect(0, 0, this.mstage.canvas.width, this.mstage.canvas.height); this.mshape.graphics.beginfill("#ff4400"); this.mshape.graphics.drawcircle(this.mstage.canvas.width / 2, this.mstage.canvas.height / 2, 25); this.mstage.addchild(this.mshape); this.mstage.update(); } } private onwindowresized (event:uievent):void { this.update(); } }
the issue have here when event executes, in context of event (window resize, dom element click, mouse overs etc), not class - this
not handle on class.
you can solve using...
window.onresize = this.onwindowresized.bind(this);
this 1 instance bind
more graceful fat-arrow syntax.
Comments
Post a Comment