Jump to content

HTML5 Image .scale(0, 0) Kills All Canvas Drawing?


JeZxLee
 Share

Recommended Posts

HTML5 Image .scale(0, 0) Kills All Canvas Drawing?

 

Hi,

 

Working on our HTML5/JS 2-D game engine now.

I found something really strange:

When we scale an HTML5 image with ".scale(0, 0)" it destroys all canvas drawing?

Not sure why?

 

Jesse

 

Here is the image drawing function:

function DrawSpriteOntoCanvas(index, x, y, scaleX, scaleY, rotationDegree, alpha, red, green, blue){    if (scaleX === 0 && scaleY === 0)  return;        var imageToDraw;    var imageToDrawWidth = 0;    var imageToDrawHeight = 0;        if (index < 101 || index > 166)    {        imageToDraw = new Image();        imageToDraw = ImageSprites[index];        imageToDrawWidth = ImageSprites[index].width;        imageToDrawHeight = ImageSprites[index].height;    }    else    {        imageToDraw = document.createElement("canvas");        imageToDraw.width = "39";        imageToDraw.height = "30";        imageToDrawWidth = 39;        imageToDrawHeight = 30;        imageToDraw = ButtonsWithCharsCanvases[index-100];    }    var computedCenterX = Math.floor(imageToDrawWidth / 2);    var computedCenterY = Math.floor(imageToDrawHeight / 2);        ctx.save();    ctx.globalAlpha = alpha;    if (rotationDegree !== 0)    {        ctx.translate(computedCenterX, computedCenterY);        ctx.rotate( DegToRad(rotationDegree) );        ctx.translate(-computedCenterX, -computedCenterY);    }    if (red !== 255 || green !== 255 || blue !== 255)    {	var buff = document.createElement("canvas");	buff.width  = imageToDrawWidth;	buff.height = imageToDrawHeight;	if (red !== 255)	{	    var ctxRGB  = buff.getContext("2d");	    ctxRGB.drawImage(imageToDraw, 0, 0);	    ctxRGB.globalAlpha = (red / 255);	    ctxRGB.globalCompositeOperation = 'source-atop';	    ctxRGB.drawImage(ImageSprites[1], 0, 0);	    	    ctxRGB.globalAlpha = 1;	    	    imageToDraw = buff;	}	if (green !== 255)	{	    var ctxRGB  = buff.getContext("2d");	    ctxRGB.drawImage(imageToDraw, 0, 0);	    ctxRGB.globalAlpha = (green / 255);	    ctxRGB.globalCompositeOperation = 'source-atop';	    ctxRGB.drawImage(ImageSprites[2], 0, 0);	    	    ctxRGB.globalAlpha = 1;	    	    imageToDraw = buff;	}	if (blue !== 255)	{	    var ctxRGB  = buff.getContext("2d");	    ctxRGB.drawImage(imageToDraw, 0, 0);	    ctxRGB.globalAlpha = (blue / 255);	    ctxRGB.globalCompositeOperation = 'source-atop';	    ctxRGB.drawImage(ImageSprites[3], 0, 0);	    	    ctxRGB.globalAlpha = 1;	    	    imageToDraw = buff;	}                buff = null;        delete buff;    }    if (scaleX !== 1 || scaleY !== 1)    {        ctx.scale(scaleX, scaleY);        if (scaleX < 0)  x = (x * -1);        if (scaleY < 0)  y = (y * -1);    }    ctx.translate(-imageToDrawWidth * 0.5, -imageToDrawHeight * 0.5);    ctx.drawImage(imageToDraw, x, y, imageToDrawWidth, imageToDrawHeight);    ctx.globalAlpha = 1;    ctx.restore();}
Link to comment
Share on other sites

But a scale of 0 is, well, nothing. It's invisible. So nothing should render. In fact you should be checking right at the start of your render loop if the object has a scale of 0 (on either axis) or an alpha of 0 and just stopping right there and moving to the next object, because it's a waste of processing time doing anything with it from that point on, and things like context saving and restore are quite expensive so you want to minimise that as much as possible.

Link to comment
Share on other sites

Hi,

 

There should be some "sanity" check by HTML5 itself?

Passing (0, 0) to ".scale" crashes the HTML5 canvas and nothing more is draw?

I added "if (scaleX === 0 || scaleY === 0) return;" to the top of my HTML5 image drawing function for now...

 

Jesse

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...