HTML5 Canvas Image Cross Fade Animation (GSAP)









up vote
0
down vote

favorite












I am new to HTML5 Canvas and I am trying to achieve a cross fade effect between two images (using GSAP) in a HTML5 canvas. I am able to fadeIn and fadeOut individually , but if I try to blend the two effects together, it doesn't work.



If you run my snippet and move between the two divs quickly, you will notice the images are changing abruptly. (However, if you hover on 1 div and move out without hovering on the other div, it fades out just fine).



I wish to combine fadeIn and fadeOut to achieve a cross fade effect (just like this https://stackoverflow.com/a/33560604/3565182) BUT with GSAP Tweens.



Help would be appreciated.






let backgroundCanvas = $('#background-canvas')[0];
let canvasContext = backgroundCanvas.getContext('2d');

/* setting height and width */
backgroundCanvas.height = 100;
backgroundCanvas.width = 150;

/* setting up images */
let image1 = new Image();
let image2 = new Image();

image1.src = "http://lorempixel.com/500/300?22";
image2.src = "http://lorempixel.com/500/300?44";

/* confirm image load */
image1.onload = () => $('body').append('Image 1 loaded -- ');
image2.onload = () => $('body').append('Image2 loaded -- ');


let currentImage;
let imageId;
let backgroundImageProps =
opacity: 0
;

/* on hover */
$('.show-image').mouseenter(function()
imageId = $(this).attr('id');

if (imageId == '1')
currentImage = image1;
else
currentImage = image2;

/* from opacity 0 to 1 */
TweenLite.to(backgroundImageProps, 2,
opacity: 1,
ease: Power4.easeOut,
onUpdate: function()
drawImage(currentImage)

);

);

/* on hover end */
$('.show-image').mouseleave(function()

let imageToHide = currentImage;

/* from opacity 1 to 0 */
TweenLite.to(backgroundImageProps, 2,
opacity: 0,
ease: Power4.easeOut,
onUpdate: function()
drawImage(imageToHide)

);

);

/* canvas drawing logic */
function drawImage(imageToDealWith)
let image = imageToDealWith;
canvasContext.save();
canvasContext.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height, 0, 0);
canvasContext.globalAlpha = backgroundImageProps.opacity;
canvasContext.drawImage(image, 0, 0, image.width, image.height, 0, 0, backgroundCanvas.width, backgroundCanvas.height);
canvasContext.restore();

canvas 
display:block;

div
display: inline-block;
border:1px solid black;
margin:10px;
padding:10px;
cursor:pointer;

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenLite.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<canvas id="background-canvas"></canvas>

<div class="show-image" id="1">Image 1 (Hover)</div>
<div class="show-image" id="2">Image 2 (Hover)</div>












share|improve this question

























    up vote
    0
    down vote

    favorite












    I am new to HTML5 Canvas and I am trying to achieve a cross fade effect between two images (using GSAP) in a HTML5 canvas. I am able to fadeIn and fadeOut individually , but if I try to blend the two effects together, it doesn't work.



    If you run my snippet and move between the two divs quickly, you will notice the images are changing abruptly. (However, if you hover on 1 div and move out without hovering on the other div, it fades out just fine).



    I wish to combine fadeIn and fadeOut to achieve a cross fade effect (just like this https://stackoverflow.com/a/33560604/3565182) BUT with GSAP Tweens.



    Help would be appreciated.






    let backgroundCanvas = $('#background-canvas')[0];
    let canvasContext = backgroundCanvas.getContext('2d');

    /* setting height and width */
    backgroundCanvas.height = 100;
    backgroundCanvas.width = 150;

    /* setting up images */
    let image1 = new Image();
    let image2 = new Image();

    image1.src = "http://lorempixel.com/500/300?22";
    image2.src = "http://lorempixel.com/500/300?44";

    /* confirm image load */
    image1.onload = () => $('body').append('Image 1 loaded -- ');
    image2.onload = () => $('body').append('Image2 loaded -- ');


    let currentImage;
    let imageId;
    let backgroundImageProps =
    opacity: 0
    ;

    /* on hover */
    $('.show-image').mouseenter(function()
    imageId = $(this).attr('id');

    if (imageId == '1')
    currentImage = image1;
    else
    currentImage = image2;

    /* from opacity 0 to 1 */
    TweenLite.to(backgroundImageProps, 2,
    opacity: 1,
    ease: Power4.easeOut,
    onUpdate: function()
    drawImage(currentImage)

    );

    );

    /* on hover end */
    $('.show-image').mouseleave(function()

    let imageToHide = currentImage;

    /* from opacity 1 to 0 */
    TweenLite.to(backgroundImageProps, 2,
    opacity: 0,
    ease: Power4.easeOut,
    onUpdate: function()
    drawImage(imageToHide)

    );

    );

    /* canvas drawing logic */
    function drawImage(imageToDealWith)
    let image = imageToDealWith;
    canvasContext.save();
    canvasContext.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height, 0, 0);
    canvasContext.globalAlpha = backgroundImageProps.opacity;
    canvasContext.drawImage(image, 0, 0, image.width, image.height, 0, 0, backgroundCanvas.width, backgroundCanvas.height);
    canvasContext.restore();

    canvas 
    display:block;

    div
    display: inline-block;
    border:1px solid black;
    margin:10px;
    padding:10px;
    cursor:pointer;

    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenLite.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <canvas id="background-canvas"></canvas>

    <div class="show-image" id="1">Image 1 (Hover)</div>
    <div class="show-image" id="2">Image 2 (Hover)</div>












    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am new to HTML5 Canvas and I am trying to achieve a cross fade effect between two images (using GSAP) in a HTML5 canvas. I am able to fadeIn and fadeOut individually , but if I try to blend the two effects together, it doesn't work.



      If you run my snippet and move between the two divs quickly, you will notice the images are changing abruptly. (However, if you hover on 1 div and move out without hovering on the other div, it fades out just fine).



      I wish to combine fadeIn and fadeOut to achieve a cross fade effect (just like this https://stackoverflow.com/a/33560604/3565182) BUT with GSAP Tweens.



      Help would be appreciated.






      let backgroundCanvas = $('#background-canvas')[0];
      let canvasContext = backgroundCanvas.getContext('2d');

      /* setting height and width */
      backgroundCanvas.height = 100;
      backgroundCanvas.width = 150;

      /* setting up images */
      let image1 = new Image();
      let image2 = new Image();

      image1.src = "http://lorempixel.com/500/300?22";
      image2.src = "http://lorempixel.com/500/300?44";

      /* confirm image load */
      image1.onload = () => $('body').append('Image 1 loaded -- ');
      image2.onload = () => $('body').append('Image2 loaded -- ');


      let currentImage;
      let imageId;
      let backgroundImageProps =
      opacity: 0
      ;

      /* on hover */
      $('.show-image').mouseenter(function()
      imageId = $(this).attr('id');

      if (imageId == '1')
      currentImage = image1;
      else
      currentImage = image2;

      /* from opacity 0 to 1 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 1,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(currentImage)

      );

      );

      /* on hover end */
      $('.show-image').mouseleave(function()

      let imageToHide = currentImage;

      /* from opacity 1 to 0 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 0,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(imageToHide)

      );

      );

      /* canvas drawing logic */
      function drawImage(imageToDealWith)
      let image = imageToDealWith;
      canvasContext.save();
      canvasContext.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height, 0, 0);
      canvasContext.globalAlpha = backgroundImageProps.opacity;
      canvasContext.drawImage(image, 0, 0, image.width, image.height, 0, 0, backgroundCanvas.width, backgroundCanvas.height);
      canvasContext.restore();

      canvas 
      display:block;

      div
      display: inline-block;
      border:1px solid black;
      margin:10px;
      padding:10px;
      cursor:pointer;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenLite.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <canvas id="background-canvas"></canvas>

      <div class="show-image" id="1">Image 1 (Hover)</div>
      <div class="show-image" id="2">Image 2 (Hover)</div>












      share|improve this question













      I am new to HTML5 Canvas and I am trying to achieve a cross fade effect between two images (using GSAP) in a HTML5 canvas. I am able to fadeIn and fadeOut individually , but if I try to blend the two effects together, it doesn't work.



      If you run my snippet and move between the two divs quickly, you will notice the images are changing abruptly. (However, if you hover on 1 div and move out without hovering on the other div, it fades out just fine).



      I wish to combine fadeIn and fadeOut to achieve a cross fade effect (just like this https://stackoverflow.com/a/33560604/3565182) BUT with GSAP Tweens.



      Help would be appreciated.






      let backgroundCanvas = $('#background-canvas')[0];
      let canvasContext = backgroundCanvas.getContext('2d');

      /* setting height and width */
      backgroundCanvas.height = 100;
      backgroundCanvas.width = 150;

      /* setting up images */
      let image1 = new Image();
      let image2 = new Image();

      image1.src = "http://lorempixel.com/500/300?22";
      image2.src = "http://lorempixel.com/500/300?44";

      /* confirm image load */
      image1.onload = () => $('body').append('Image 1 loaded -- ');
      image2.onload = () => $('body').append('Image2 loaded -- ');


      let currentImage;
      let imageId;
      let backgroundImageProps =
      opacity: 0
      ;

      /* on hover */
      $('.show-image').mouseenter(function()
      imageId = $(this).attr('id');

      if (imageId == '1')
      currentImage = image1;
      else
      currentImage = image2;

      /* from opacity 0 to 1 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 1,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(currentImage)

      );

      );

      /* on hover end */
      $('.show-image').mouseleave(function()

      let imageToHide = currentImage;

      /* from opacity 1 to 0 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 0,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(imageToHide)

      );

      );

      /* canvas drawing logic */
      function drawImage(imageToDealWith)
      let image = imageToDealWith;
      canvasContext.save();
      canvasContext.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height, 0, 0);
      canvasContext.globalAlpha = backgroundImageProps.opacity;
      canvasContext.drawImage(image, 0, 0, image.width, image.height, 0, 0, backgroundCanvas.width, backgroundCanvas.height);
      canvasContext.restore();

      canvas 
      display:block;

      div
      display: inline-block;
      border:1px solid black;
      margin:10px;
      padding:10px;
      cursor:pointer;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenLite.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <canvas id="background-canvas"></canvas>

      <div class="show-image" id="1">Image 1 (Hover)</div>
      <div class="show-image" id="2">Image 2 (Hover)</div>








      let backgroundCanvas = $('#background-canvas')[0];
      let canvasContext = backgroundCanvas.getContext('2d');

      /* setting height and width */
      backgroundCanvas.height = 100;
      backgroundCanvas.width = 150;

      /* setting up images */
      let image1 = new Image();
      let image2 = new Image();

      image1.src = "http://lorempixel.com/500/300?22";
      image2.src = "http://lorempixel.com/500/300?44";

      /* confirm image load */
      image1.onload = () => $('body').append('Image 1 loaded -- ');
      image2.onload = () => $('body').append('Image2 loaded -- ');


      let currentImage;
      let imageId;
      let backgroundImageProps =
      opacity: 0
      ;

      /* on hover */
      $('.show-image').mouseenter(function()
      imageId = $(this).attr('id');

      if (imageId == '1')
      currentImage = image1;
      else
      currentImage = image2;

      /* from opacity 0 to 1 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 1,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(currentImage)

      );

      );

      /* on hover end */
      $('.show-image').mouseleave(function()

      let imageToHide = currentImage;

      /* from opacity 1 to 0 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 0,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(imageToHide)

      );

      );

      /* canvas drawing logic */
      function drawImage(imageToDealWith)
      let image = imageToDealWith;
      canvasContext.save();
      canvasContext.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height, 0, 0);
      canvasContext.globalAlpha = backgroundImageProps.opacity;
      canvasContext.drawImage(image, 0, 0, image.width, image.height, 0, 0, backgroundCanvas.width, backgroundCanvas.height);
      canvasContext.restore();

      canvas 
      display:block;

      div
      display: inline-block;
      border:1px solid black;
      margin:10px;
      padding:10px;
      cursor:pointer;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenLite.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <canvas id="background-canvas"></canvas>

      <div class="show-image" id="1">Image 1 (Hover)</div>
      <div class="show-image" id="2">Image 2 (Hover)</div>





      let backgroundCanvas = $('#background-canvas')[0];
      let canvasContext = backgroundCanvas.getContext('2d');

      /* setting height and width */
      backgroundCanvas.height = 100;
      backgroundCanvas.width = 150;

      /* setting up images */
      let image1 = new Image();
      let image2 = new Image();

      image1.src = "http://lorempixel.com/500/300?22";
      image2.src = "http://lorempixel.com/500/300?44";

      /* confirm image load */
      image1.onload = () => $('body').append('Image 1 loaded -- ');
      image2.onload = () => $('body').append('Image2 loaded -- ');


      let currentImage;
      let imageId;
      let backgroundImageProps =
      opacity: 0
      ;

      /* on hover */
      $('.show-image').mouseenter(function()
      imageId = $(this).attr('id');

      if (imageId == '1')
      currentImage = image1;
      else
      currentImage = image2;

      /* from opacity 0 to 1 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 1,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(currentImage)

      );

      );

      /* on hover end */
      $('.show-image').mouseleave(function()

      let imageToHide = currentImage;

      /* from opacity 1 to 0 */
      TweenLite.to(backgroundImageProps, 2,
      opacity: 0,
      ease: Power4.easeOut,
      onUpdate: function()
      drawImage(imageToHide)

      );

      );

      /* canvas drawing logic */
      function drawImage(imageToDealWith)
      let image = imageToDealWith;
      canvasContext.save();
      canvasContext.clearRect(0, 0, backgroundCanvas.width, backgroundCanvas.height, 0, 0);
      canvasContext.globalAlpha = backgroundImageProps.opacity;
      canvasContext.drawImage(image, 0, 0, image.width, image.height, 0, 0, backgroundCanvas.width, backgroundCanvas.height);
      canvasContext.restore();

      canvas 
      display:block;

      div
      display: inline-block;
      border:1px solid black;
      margin:10px;
      padding:10px;
      cursor:pointer;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenLite.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <canvas id="background-canvas"></canvas>

      <div class="show-image" id="1">Image 1 (Hover)</div>
      <div class="show-image" id="2">Image 2 (Hover)</div>






      javascript html5 canvas gasp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 10:43









      saibbyweb

      3831317




      3831317



























          active

          oldest

          votes











          Your Answer






          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: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247945%2fhtml5-canvas-image-cross-fade-animation-gsap%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247945%2fhtml5-canvas-image-cross-fade-animation-gsap%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto