Skip to main content
Beardmancer
Known Participant
December 4, 2016
Answered

Scale a movie clip dynamically using JS (canvas document)

  • December 4, 2016
  • 2 replies
  • 6086 views

Hello all,

I am coming back to Animate CC after using Flash many years ago. Currently, I'm playing with a fullscreen page based on Joseph Labrecque's excellent demo.

Original from Joseph: http://inflagrantedelicto.memoryspiral.com/2016/02/responsive-scaling-in-animate-cc/

My example: http://iantompkins.com/animate/responsive3.html

So far, I have managed to modify the published html so that the canvas size is changed the width and height of the window when the page loads. Joseph's code does a good job pinning the movie clips to certain places in the window.

What I need now is to be able to change the width and height of a movie clip at different window sizes, much like breakpoints on a responsive site. You can see in my example that I have added a translucent white box. Ideally, I would like this box to be the size of the window, and I would load content into it when a button is clicked. Is there a way to alter a movie clip's dimensions with javascript?

I have tried something like this on frame 1, where "win1" is the instance name of the white box, but this was honestly just a shot in the dark:

var r = this;

var objw = stage.canvas.width;

var objh = stage.canvas.height;

r.win1.width = objw;

r.win1.height = objh;

Any ideas on what I could try next?

This topic has been closed for replies.
Correct answer ClayUUID

There's no such instance properties as .width and .height in Canvas mode. You set .scaleX and .scaleY to resize objects. For your use case I'd recommend making the clip's native dimensions 100x100 so the math is as straightforward as possible. When that isn't possible you can use a clip's .nominalBounds.width and .nominalBounds.height to determine the scale factor.

EaselJS v0.8.2 API Documentation : DisplayObject

2 replies

ClayUUIDCorrect answer
Legend
December 5, 2016

There's no such instance properties as .width and .height in Canvas mode. You set .scaleX and .scaleY to resize objects. For your use case I'd recommend making the clip's native dimensions 100x100 so the math is as straightforward as possible. When that isn't possible you can use a clip's .nominalBounds.width and .nominalBounds.height to determine the scale factor.

EaselJS v0.8.2 API Documentation : DisplayObject

Beardmancer
Known Participant
December 5, 2016

Aha, I think this is what I'm looking for. I'm going to give it a shot and report back. Thanks!

ClayUUID wrote:

There's no such instance properties as .width and .height in Canvas mode. You set .scaleX and .scaleY to resize objects. For your use case I'd recommend making the clip's native dimensions 100x100 so the math is as straightforward as possible. When that isn't possible you can use a clip's .nominalBounds.width and .nominalBounds.height to determine the scale factor.

EaselJS v0.8.2 API Documentation : DisplayObject

kglad
Community Expert
Community Expert
December 4, 2016

what's not working when using the default responsive setting?

Beardmancer
Known Participant
December 4, 2016

When I include those settings, publish, and change the html file to make the canvas fill the window, it behaves the same as without the settings checked. The translucent white box still does not scale, but rather bleeds off the edge of the window. Here is an example: http://iantompkins.com/animate/responsive4.html