Skip to main content
patrickb89495570
Known Participant
October 17, 2018
Answered

Sticky object with animate HTML5

  • October 17, 2018
  • 2 replies
  • 560 views

Hello,

Is there a way for an object to be sticky in animate HTML5 ?

I searched the answer... but not a single clue

Thanks a lot

This topic has been closed for replies.
Correct answer StephaneHH

The short answer is that this is a 2-step process because there are 2 contexts.

1. The context of the browser in which the <canvas> is positioned.

2. The context of the <canvas> in which your sticky-element is positioned.

So in order to position your sticky-element relative to the browser-context, you need to do these steps:

1. Find the position of the <canvas> on the screen (the inner-window of the browser).

2. Find the position of the sticky-element within the canvas.

3. Combine these two in order to get the position of your sticky-element relative to the browser.

I hope this makes sense.

A small and hopefully useful code snippet:

Below I have pasted some code I have used to get the position of a DOM element within the browser context.

You would wrap this in a function that is triggered by scroll and resize:

window.addEventListener("scroll", MyFunction);

window.addEventListener("resize", MyFunction);

I will leave the positioning inside Animate up to you

-------------------------------

// Find window inner height:

this.pageHeight = window.innerHeight;

// Get the element and its bounds:

this.ele = [The <canvas> DOM element]

this.bcr = this.ele.getBoundingClientRect();

// Find the position of the top pixel of the <canvas> in PIXELS and in PERCENT:

this.calcY = this.pageHeight - this.bcr.top;

this.pctY = this.calcY / this.pageHeight;

-------------------------------

2 replies

patrickb89495570
Known Participant
December 18, 2018

It's really perfect !!!!!

Thanks a lot for your answer and time

StephaneHH
Participant
October 17, 2018

Doing this with a canvas element may takes some extra steps compared to doing it with DOM elements (dependent on what you want to do).

- Is it the entire <canvas> you want to stick or is it some element within the <canvas>?

patrickb89495570
Known Participant
October 17, 2018

Hello Stephane,

it's some element within the <canvas>

StephaneHH
StephaneHHCorrect answer
Participant
October 17, 2018

The short answer is that this is a 2-step process because there are 2 contexts.

1. The context of the browser in which the <canvas> is positioned.

2. The context of the <canvas> in which your sticky-element is positioned.

So in order to position your sticky-element relative to the browser-context, you need to do these steps:

1. Find the position of the <canvas> on the screen (the inner-window of the browser).

2. Find the position of the sticky-element within the canvas.

3. Combine these two in order to get the position of your sticky-element relative to the browser.

I hope this makes sense.

A small and hopefully useful code snippet:

Below I have pasted some code I have used to get the position of a DOM element within the browser context.

You would wrap this in a function that is triggered by scroll and resize:

window.addEventListener("scroll", MyFunction);

window.addEventListener("resize", MyFunction);

I will leave the positioning inside Animate up to you

-------------------------------

// Find window inner height:

this.pageHeight = window.innerHeight;

// Get the element and its bounds:

this.ele = [The <canvas> DOM element]

this.bcr = this.ele.getBoundingClientRect();

// Find the position of the top pixel of the <canvas> in PIXELS and in PERCENT:

this.calcY = this.pageHeight - this.bcr.top;

this.pctY = this.calcY / this.pageHeight;

-------------------------------