Abstract water ripple animation on an HTML canvas

The name of the pictureThe name of the pictureThe name of the pictureClash 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)







share|improve this question





















  • 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










  • 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
















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)







share|improve this question





















  • 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










  • 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












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)







share|improve this question













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)









share|improve this question












share|improve this question




share|improve this question








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










  • @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










  • 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










  • @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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?