Abstract water ripple animation on an HTML canvas
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I am curious to know whether this is the most basic and best way I could be coding this animation on my website object thats a lone tree on an island when you click on it a nice animation plays HERE.
There are condisions that have to be met with this animation.
Has to be responsive for all devices
Smooth animations that dont take up too much memory
No CSS, I have made this object twice in CSS and making it responsive for other devices is a nightmare.
I also wanted to add dates and weather to the islands grass if anyone knows how I can manage that without using a canvas I am severely intrigued. Just as a foot note I'm not asking for code I'm asking for advice on another way or a better form of practice that could provide me with more functionality or even less code.
////// MAIN \\\
ripple.onload = function()
canvas.width = window.innerWidth;
canvas.height = img.height * 0.5;
for (i = 0; i < imageArray.length; i++)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
// IDLE ANIMATION
function rippleAnimation()
setTimeout(function()
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
if (!stopFirstAnimation)rippleAnimation();
, 1000/fps)
// CLICKED ANIMATION
function animation()
stopFirstAnimation = true;
setTimeout(function()
mill = mill + 1.5;
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
//RIPPLE ANIMATION
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
//FIRST STEP
else if(mill < 22)
//SECOND STEP
else if (mill >= 22 && mill <= 66) imageArray[i] == img2)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, mill, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
//RENDER FINISH
//CLEAR TREE
else imageArray[i] == img4)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66-22, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else if(imageArray[i] == tree)
treeOpacity = treeOpacity - 0.04;
if (treeOpacity < 0)
treeOpacity = 0;
con.save();
con.globalAlpha = treeOpacity;
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
con.restore();
else if(imageArray[i] == img
animation();
, 1000/fps)
javascript html animation
add a comment |Â
up vote
0
down vote
favorite
I am curious to know whether this is the most basic and best way I could be coding this animation on my website object thats a lone tree on an island when you click on it a nice animation plays HERE.
There are condisions that have to be met with this animation.
Has to be responsive for all devices
Smooth animations that dont take up too much memory
No CSS, I have made this object twice in CSS and making it responsive for other devices is a nightmare.
I also wanted to add dates and weather to the islands grass if anyone knows how I can manage that without using a canvas I am severely intrigued. Just as a foot note I'm not asking for code I'm asking for advice on another way or a better form of practice that could provide me with more functionality or even less code.
////// MAIN \\\
ripple.onload = function()
canvas.width = window.innerWidth;
canvas.height = img.height * 0.5;
for (i = 0; i < imageArray.length; i++)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
// IDLE ANIMATION
function rippleAnimation()
setTimeout(function()
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
if (!stopFirstAnimation)rippleAnimation();
, 1000/fps)
// CLICKED ANIMATION
function animation()
stopFirstAnimation = true;
setTimeout(function()
mill = mill + 1.5;
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
//RIPPLE ANIMATION
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
//FIRST STEP
else if(mill < 22)
//SECOND STEP
else if (mill >= 22 && mill <= 66) imageArray[i] == img2)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, mill, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
//RENDER FINISH
//CLEAR TREE
else imageArray[i] == img4)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66-22, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else if(imageArray[i] == tree)
treeOpacity = treeOpacity - 0.04;
if (treeOpacity < 0)
treeOpacity = 0;
con.save();
con.globalAlpha = treeOpacity;
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
con.restore();
else if(imageArray[i] == img
animation();
, 1000/fps)
javascript html animation
Have you considered animating SVGs?
â le_m
Jan 28 at 13:30
You should never usesetTimeout
, orsetInterval
to time and render any animated DOM content for too many reasons to be explained in a comment. UserequestAnimationFrame
to ensure correct presentation / timing in sync with the display hardware, and maintain optimal resource usage to reduce power consumption and maximize battery life.
â Blindman67
Jan 28 at 17:37
Thank you so very much Blindman67 I will have a crack at changing it, and le_m I am in the process of changing them to svg files but thank you for mentioning it.
â Adam Brickhill
Jan 30 at 17:02
@Blindman67 Comments are for seeking clarification to the question. All suggestions for improvements should be put into answers, even if the answer if brief.
â 200_success
Jan 30 at 18:08
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am curious to know whether this is the most basic and best way I could be coding this animation on my website object thats a lone tree on an island when you click on it a nice animation plays HERE.
There are condisions that have to be met with this animation.
Has to be responsive for all devices
Smooth animations that dont take up too much memory
No CSS, I have made this object twice in CSS and making it responsive for other devices is a nightmare.
I also wanted to add dates and weather to the islands grass if anyone knows how I can manage that without using a canvas I am severely intrigued. Just as a foot note I'm not asking for code I'm asking for advice on another way or a better form of practice that could provide me with more functionality or even less code.
////// MAIN \\\
ripple.onload = function()
canvas.width = window.innerWidth;
canvas.height = img.height * 0.5;
for (i = 0; i < imageArray.length; i++)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
// IDLE ANIMATION
function rippleAnimation()
setTimeout(function()
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
if (!stopFirstAnimation)rippleAnimation();
, 1000/fps)
// CLICKED ANIMATION
function animation()
stopFirstAnimation = true;
setTimeout(function()
mill = mill + 1.5;
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
//RIPPLE ANIMATION
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
//FIRST STEP
else if(mill < 22)
//SECOND STEP
else if (mill >= 22 && mill <= 66) imageArray[i] == img2)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, mill, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
//RENDER FINISH
//CLEAR TREE
else imageArray[i] == img4)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66-22, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else if(imageArray[i] == tree)
treeOpacity = treeOpacity - 0.04;
if (treeOpacity < 0)
treeOpacity = 0;
con.save();
con.globalAlpha = treeOpacity;
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
con.restore();
else if(imageArray[i] == img
animation();
, 1000/fps)
javascript html animation
I am curious to know whether this is the most basic and best way I could be coding this animation on my website object thats a lone tree on an island when you click on it a nice animation plays HERE.
There are condisions that have to be met with this animation.
Has to be responsive for all devices
Smooth animations that dont take up too much memory
No CSS, I have made this object twice in CSS and making it responsive for other devices is a nightmare.
I also wanted to add dates and weather to the islands grass if anyone knows how I can manage that without using a canvas I am severely intrigued. Just as a foot note I'm not asking for code I'm asking for advice on another way or a better form of practice that could provide me with more functionality or even less code.
////// MAIN \\\
ripple.onload = function()
canvas.width = window.innerWidth;
canvas.height = img.height * 0.5;
for (i = 0; i < imageArray.length; i++)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
// IDLE ANIMATION
function rippleAnimation()
setTimeout(function()
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
if (!stopFirstAnimation)rippleAnimation();
, 1000/fps)
// CLICKED ANIMATION
function animation()
stopFirstAnimation = true;
setTimeout(function()
mill = mill + 1.5;
rippleMill++;
opacity = opacity - 0.005;
con.clearRect(0, 0, con.canvas.width, con.canvas.height);
for (i = 0; i < imageArray.length; i++)
//RIPPLE ANIMATION
if (imageArray[i] == ripple)
if (opacity < 0)
opacity = 1;
rippleMill = 0;
con.save();
con.globalAlpha = opacity;
con.drawImage(ripple,
halfScreen - ripple.width * 0.5 * 0.5 - rippleMill / 2, - rippleMill / 2, img.naturalWidth * 0.5 + rippleMill, img.naturalHeight * 0.5 + rippleMill);
con.restore();
//FIRST STEP
else if(mill < 22)
//SECOND STEP
else if (mill >= 22 && mill <= 66) imageArray[i] == img2)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, mill, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 0, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
//RENDER FINISH
//CLEAR TREE
else imageArray[i] == img4)
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66-22, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
else if(imageArray[i] == tree)
treeOpacity = treeOpacity - 0.04;
if (treeOpacity < 0)
treeOpacity = 0;
con.save();
con.globalAlpha = treeOpacity;
con.drawImage(imageArray[i], halfScreen - imageArray[i].width * 0.5 * 0.5, 66, imageArray[i].width * 0.5, imageArray[i].height * 0.5);
con.restore();
else if(imageArray[i] == img
animation();
, 1000/fps)
javascript html animation
edited Jan 30 at 18:07
200_success
123k14143401
123k14143401
asked Jan 28 at 3:15
Adam Brickhill
61
61
Have you considered animating SVGs?
â le_m
Jan 28 at 13:30
You should never usesetTimeout
, orsetInterval
to time and render any animated DOM content for too many reasons to be explained in a comment. UserequestAnimationFrame
to ensure correct presentation / timing in sync with the display hardware, and maintain optimal resource usage to reduce power consumption and maximize battery life.
â Blindman67
Jan 28 at 17:37
Thank you so very much Blindman67 I will have a crack at changing it, and le_m I am in the process of changing them to svg files but thank you for mentioning it.
â Adam Brickhill
Jan 30 at 17:02
@Blindman67 Comments are for seeking clarification to the question. All suggestions for improvements should be put into answers, even if the answer if brief.
â 200_success
Jan 30 at 18:08
add a comment |Â
Have you considered animating SVGs?
â le_m
Jan 28 at 13:30
You should never usesetTimeout
, orsetInterval
to time and render any animated DOM content for too many reasons to be explained in a comment. UserequestAnimationFrame
to ensure correct presentation / timing in sync with the display hardware, and maintain optimal resource usage to reduce power consumption and maximize battery life.
â Blindman67
Jan 28 at 17:37
Thank you so very much Blindman67 I will have a crack at changing it, and le_m I am in the process of changing them to svg files but thank you for mentioning it.
â Adam Brickhill
Jan 30 at 17:02
@Blindman67 Comments are for seeking clarification to the question. All suggestions for improvements should be put into answers, even if the answer if brief.
â 200_success
Jan 30 at 18:08
Have you considered animating SVGs?
â le_m
Jan 28 at 13:30
Have you considered animating SVGs?
â le_m
Jan 28 at 13:30
You should never use
setTimeout
, or setInterval
to time and render any animated DOM content for too many reasons to be explained in a comment. Use requestAnimationFrame
to ensure correct presentation / timing in sync with the display hardware, and maintain optimal resource usage to reduce power consumption and maximize battery life.â Blindman67
Jan 28 at 17:37
You should never use
setTimeout
, or setInterval
to time and render any animated DOM content for too many reasons to be explained in a comment. Use requestAnimationFrame
to ensure correct presentation / timing in sync with the display hardware, and maintain optimal resource usage to reduce power consumption and maximize battery life.â Blindman67
Jan 28 at 17:37
Thank you so very much Blindman67 I will have a crack at changing it, and le_m I am in the process of changing them to svg files but thank you for mentioning it.
â Adam Brickhill
Jan 30 at 17:02
Thank you so very much Blindman67 I will have a crack at changing it, and le_m I am in the process of changing them to svg files but thank you for mentioning it.
â Adam Brickhill
Jan 30 at 17:02
@Blindman67 Comments are for seeking clarification to the question. All suggestions for improvements should be put into answers, even if the answer if brief.
â 200_success
Jan 30 at 18:08
@Blindman67 Comments are for seeking clarification to the question. All suggestions for improvements should be put into answers, even if the answer if brief.
â 200_success
Jan 30 at 18:08
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186174%2fabstract-water-ripple-animation-on-an-html-canvas%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Have you considered animating SVGs?
â le_m
Jan 28 at 13:30
You should never use
setTimeout
, orsetInterval
to time and render any animated DOM content for too many reasons to be explained in a comment. UserequestAnimationFrame
to ensure correct presentation / timing in sync with the display hardware, and maintain optimal resource usage to reduce power consumption and maximize battery life.â Blindman67
Jan 28 at 17:37
Thank you so very much Blindman67 I will have a crack at changing it, and le_m I am in the process of changing them to svg files but thank you for mentioning it.
â Adam Brickhill
Jan 30 at 17:02
@Blindman67 Comments are for seeking clarification to the question. All suggestions for improvements should be put into answers, even if the answer if brief.
â 200_success
Jan 30 at 18:08