“…i want only a rectnagle already drawn to save it as image.”
This can be read at least 2 ways:
- I want to draw a rectangle on canvas and convert just that rectangle to an image.
- I have already drawn a rectangle on the canvas and I want to save just that rect as an image.
Here’s how to do #2 assuming a 26x26 rect + 1px stroke is already on the canvas at xy=20,20.
This code "clips" the existing rectangle out of an existing canvas.
// create a temporary canvas
var tempcanvas=document.createElement("canvas");
var tempctx=tempcanvas.getContext("2d");
// size the tempcanvas to the rect width plus stroke width
tempcanvas.width=28;
tempcanvas.height=28;
// use drawImage to clip the rect from the existing canvas to the tempcanvas
tempctx.drawImage(canvas,19,19,28,28,0,0,28,28);
// save the tempcanvas as an image using toDataURL()
// For example, use toDataURL as .src to an html img element with id=”clipped”
document.getElementById("clipped").src=tempcanvas.toDataURL();
Here’s how to do #1.
This code creates a new 28x28 temporary canvas and draws a rectangle on it
// create a temporary canvas
var tempcanvas1=document.createElement("canvas");
var tempctx1=tempcanvas1.getContext("2d");
// size the tempcanvas to the rect width plus stroke width
tempcanvas1.width=28;
tempcanvas1.height=28;
// draw a rect on the temp canvas
tempctx1.beginPath();
tempctx1.rect(0,0,26,26);
tempctx1.fillStyle="orange";
tempctx1.strokeStyle="lightgray";
tempctx1.fill();
tempctx1.lineWidth=1;
tempctx1.stroke();
// save the tempcanvas as an image using toDataURL()
// For example, use toDataURL as .src to an html img element with id=”drawn”
document.getElementById("drawn").src=tempcanvas.toDataURL();
Here’s code and a Fiddle: http://jsfiddle.net/m1erickson/KrNJ7/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; padding:20px; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
// draw a rect on a canvas
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.beginPath();
ctx.rect(20,20,26,26);
ctx.fillStyle="orange";
ctx.strokeStyle="blue";
ctx.fill();
ctx.lineWidth=1;
ctx.stroke();
// clip the rect out of the canvas and save as an image
var tempcanvas=document.createElement("canvas");
var tempctx=tempcanvas.getContext("2d");
tempcanvas.width=28;
tempcanvas.height=28;
tempctx.drawImage(canvas,19,19,28,28,0,0,28,28);
document.getElementById("clipped").src=tempcanvas.toDataURL();
// create a new rect on a temp canvas and save as an image
var tempcanvas1=document.createElement("canvas");
var tempctx1=tempcanvas1.getContext("2d");
tempctx1.beginPath();
tempctx1.rect(0,0,26,26);
tempctx1.fillStyle="orange";
tempctx1.strokeStyle="lightgray";
tempctx1.fill();
tempctx1.lineWidth=1;
tempctx1.stroke();
document.getElementById("drawn").src=tempcanvas.toDataURL();
}); // end $(function(){});
</script>
</head>
<body>
<p>A rect predrawn on a canvas</p>
<canvas id="canvas" width=50 height=50></canvas>
<p>An image of just the rect clipped from the canvas above</p>
<img id="clipped" width=28 height=28>
<p>An image of just a rect created on a temp canvas</p>
<img id="drawn" width=26 height=26>
</body>
</html>