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

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

Community Beginner ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

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

Views

198

Translate

Translate

Report

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(
...

Votes

Translate

Translate
Community Expert ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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);

};

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

swipe() function looks reversed.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

This is due to nature of the swipe gesture.

 

You can check the live preview for yourself.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

 

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Swiping to the left should bring the next frame.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

it does.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thank you!! It works perfectly. 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

You're welcome!

Votes

Translate

Translate

Report

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