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>
javascript html5 canvas gasp
add a comment |
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>
javascript html5 canvas gasp
add a comment |
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>
javascript html5 canvas gasp
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
javascript html5 canvas gasp
asked Nov 11 at 10:43
saibbyweb
3831317
3831317
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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