01271 Posted July 20, 2018 Share Posted July 20, 2018 Hey all, I want to resize an element but not as often as I currently do it. Right now I have a project with an html element that gets resized on every frame and it runs on every update. game.gui.TextArea = me.Renderable.extend({ init: function() { this.textarea = document.createElement('textarea'); this.textarea.id = 'textHistory'; this.textarea.readOnly = "true"; this.textarea.disabled = "true"; me.video.getWrapper().appendChild(this.textarea); }, update: function() { videoPos = me.video.getPos(); this.textarea.style = 'width:' + (window.innerWidth - videoPos.width) + 'px; height:' + videoPos.height + 'px;'; } }); I'm concerned about this, this code will run way more often than it needs. Not sure about the performance impact but it only needs to run when the window size is updated. Is there a way I can piggyback on any melonjs functions that resize the canvas and only run it then? Quote Link to comment Share on other sites More sharing options...
obiot Posted July 21, 2018 Share Posted July 21, 2018 Hello, you should be able to use the WINDOW_ONRESIZE event, that is triggered every time the display is resize, so for example : var self = this; me.event.subscribe(me.event.WINDOW_ONRESIZE, function () { var videoPos = me.video.getPos(); self.textarea.style = 'width:' + (window.innerWidth - videoPos.width) + 'px; height:' + videoPos.height + 'px;'; }); also the size you can also directly use the default game camera/viewport (me.game.viewport.width and me.game.viewport.height) that is also automatically resize to match with the canvas size. 01271 1 Quote Link to comment Share on other sites More sharing options...
Parasyte Posted July 21, 2018 Share Posted July 21, 2018 There's also VIEWPORT_ONRESIZE. Depending on which event you really care about. But the event handler is definitely the right way to handle this. Quote Link to comment Share on other sites More sharing options...
obiot Posted July 21, 2018 Share Posted July 21, 2018 Indeed VIEWPORT_RESIZE is probably better, and if i’m correct the new width and height are even passed as parameters. http://melonjs.github.io/melonJS/docs/me.event.html#VIEWPORT_ONRESIZE Quote Link to comment Share on other sites More sharing options...
01271 Posted July 22, 2018 Author Share Posted July 22, 2018 Thanks for that, I was looking in the classes and I totally missed out on the events, that one with the window onresize works very well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.