Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looking for Javascript to create a swipe function on an html5 canvas

Community Beginner ,
Mar 05, 2024 Mar 05, 2024

I have an animate document with multiple frames. Each frame contains a slide with some content on it (basically like a simple presentation) and you can navigate between the frames with arrows (buttons). 

Now I want to add a function which enables a swipe function (for mobile devices) so you can just swipe to get to the next or previouse frame. 

 

How do I make this work? 

(I've already tried multiple codes I found online but nothing seems to work...) 

 

Some help would be appreciated! Thank you. 

TOPICS
Code
682
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 08, 2024 Mar 08, 2024

Hi.

 

Here is an example.

Live preview:

https://bit.ly/49HdRDb

Javascript:

var swipeTolerance = 30;
var pressedX;

function main()
{
	createjs.Touch.enable(stage);
	root.loop = false;
	root.stop();
	
	root.on("click", onClick);

	stage.on("stagemousedown", onStageMouseDown);
	stage.on("stagemouseup", onStageMouseUp);
}

function onClick(e)
{
	switch(e.target.name)
	{
		case "prevButton":
			prevFrame();
			break;
		case "nextButton":
			nextFrame();
			break;
	}
}

function onStageMouseDown(
...
Translate
Community Expert ,
Mar 05, 2024 Mar 05, 2024
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2024 Mar 07, 2024

Thanks!
Still, I cant seem to get it work... I'm the worst at coding, so I've got no idea on how to make that work. :')

I've tried this for example:

 

canvas.addEventListener("ontouchmove", fl_MobileSwipe.bind(canvas));
function fl_MobileSwipe ()
{

{

if (this.navigationLoop && this.currentFrame == 0)

this.gotoAndStop(this.timeline.duration - 1);

else

this.gotoAndStop(this.currentFrame - 1);

};

this.nextFrame = function(e)

{

if (!this.navigationLoop && this.currentFrame == this.timeline.duration - 1)

return;

this.gotoAndStop(this.currentFrame + 1);

};

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Hi.

 

Here is an example.

Live preview:

https://bit.ly/49HdRDb

Javascript:

var swipeTolerance = 30;
var pressedX;

function main()
{
	createjs.Touch.enable(stage);
	root.loop = false;
	root.stop();
	
	root.on("click", onClick);

	stage.on("stagemousedown", onStageMouseDown);
	stage.on("stagemouseup", onStageMouseUp);
}

function onClick(e)
{
	switch(e.target.name)
	{
		case "prevButton":
			prevFrame();
			break;
		case "nextButton":
			nextFrame();
			break;
	}
}

function onStageMouseDown(e)
{
	pressedX = e.stageX;
}

function onStageMouseUp(e)
{
	var dragOffsetX = e.stageX - pressedX;
		
	if (Math.abs(dragOffsetX) >= swipeTolerance)
		swipe(dragOffsetX > 0 ? 1 : -1);
}

function swipe(direction)
{
	if (direction === 1)
		prevFrame();
	else if (direction === -1)
		nextFrame();
}

function prevFrame()
{
	root.gotoAndStop(root.currentFrame - 1);
}

function nextFrame()
{
	root.gotoAndStop(root.currentFrame + 1);
}

window.root = this;

if (!root.started)
{
	main();
	root.started = true;
}

 

Source / download / files / FLA:
https://bit.ly/3Ts9JB8


I hope this helps.

Regards,
JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

swipe() function looks reversed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

This is due to nature of the swipe gesture.

 

You can check the live preview for yourself.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

the live preview is reversed when tested (using chrome and edge) using a mouse drag. eg, left to right, decreases the numbers.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Swiping to the left should bring the next frame.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

it does.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Yeah. Pretty much every gallery works by swiping/dragging to the left to bring the next photo.

 

And remember that the numbers in the example are frames, so they should increase when dragging to the left.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2024 Mar 08, 2024

it seemed counter-intuitive to me, but that is the way it should work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 11, 2024 Mar 11, 2024

Thank you!! It works perfectly. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2024 Mar 11, 2024
LATEST

You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines