Canvas resize question

D

DL

It seems pretty straight-forward to simply resize a canvas, code like
thie following would do:
var c = document.getElementById('canvas');
c.height = c.height + 15;
c.width = c.width + 15;

However, this resized canvas would appear empty.

How does it work? Could I create a hidden element, a) upon the resize
event trigger a copy of the canvas element to the hidden element; b)
then do the canvas resizing and c) then copy it/them back to the
canvas?

And if the above approach is the right way?
Step one seems critical,
// a) copy the original canvas content to the temp element named
canvastemp
var canvastemp = document.getElementById("canvastemp");
var ctemp = canvastemp.getContext("2d");
// duplicate the temp object from the original canvas object
canvastemp = c;

Not working, what's wrong?

Thanks.
 
T

Trevor Lawrence

DL said:
It seems pretty straight-forward to simply resize a canvas, code like
thie following would do:
var c = document.getElementById('canvas');
c.height = c.height + 15;
c.width = c.width + 15;

However, this resized canvas would appear empty.

How does it work? Could I create a hidden element, a) upon the resize
event trigger a copy of the canvas element to the hidden element; b)
then do the canvas resizing and c) then copy it/them back to the
canvas?

And if the above approach is the right way?
Step one seems critical,
// a) copy the original canvas content to the temp element named
canvastemp
var canvastemp = document.getElementById("canvastemp");
var ctemp = canvastemp.getContext("2d");
// duplicate the temp object from the original canvas object
canvastemp = c;

Not working, what's wrong?


I have been trying similar things where I have a <div> into which I place an
image. I resize both the image and the <div>. I am making a few errors along
the way, but generally, it seems to be working fairly well.

It may help to know what the element is which has id='canvas'.

Is it a <div> as in my case?
Or is it an <img>?
Does it contain something to start with ?

Seeing the rest of the code (e.g. the HTML that has id='canvas') may also
help
 
D

DL

I have been trying similar things where I have a <div> into which I placean
image. I resize both the image and the <div>. I am making a few errors along
the way, but generally, it seems to be working fairly well.

It may help to know what the element is which has id='canvas'.

Is it a <div> as in my case?
Or is it an <img>?
Does it contain something to start with ?

Seeing the rest of the code (e.g. the HTML that has id='canvas') may also
help
--
Trevor Lawrence
Canberra
Web Sitehttp://trevorl.mvps.org- Hide quoted text -

- Show quoted text -

That's encouraging.

The HTML part looks like this:
....
<body>
<canvas width="300" height="300" style="border:1px solid black;"
id="canvas"></canvas>
<input type="hidden" id="canvastemp"> <br/>
<input type="button" id="canvasResize" onclick="canvasResize();"
value=" + ">
....

The js part looks like this:
function canvasResize() {
var can = document.getElementById('canvas');
// a) copy canvas to temp
var canvastemp = document.getElementById("canvastemp");
var ctemp = canvastemp.getContext("2d");

// duplicate content
canvastemp = can;

// b) resize canvas
can.height = can.height + 15;
can.width = can.width + 15;

// c) copy back canvas content
can = canvastemp;
cxt = can.getContext("2d");
cxt.style.display = "block";

}


Or another way around, how does yours work?

Many thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top