I'm trying to capture a div into an image using html5 canvas. Where I've a background image of that div so when trying to download the image in jpeg it is download without the background image. I'm sure I've that image in my domain so no way to break the same origin rule
HTML
<div id="testdiv" style="background:(path/to/image) no-repat">
</div>
JS
$(document).ready(function() {
//var testdiv = document.getElementById("testdiv");
html2canvas($("#testdiv"), {
onrendered: function(canvas) {
// canvas is the final rendered <canvas> element
var myImage = canvas.toDataURL("image/png");
window.open(myImage);
}
});
});
Why it's now drawing the background image of the div on canvas?
Any help? Thanks
You have to set css height to your div#testdiv like :
<div id="testdiv" style="background:(path/to/image) no-repeat; height:700px"></div>
Related
I was making a game using HTML5 canvas. Halfway through making my game, I decided to change my game sprite into a spaceship. But as I soon learned, you need to have an image tag to use the c.drawImage function and I did not want to include an image tag in my HTML because it would ruin the margin and padding around my game and I did not want random image dotted around my game just to use a new sprite.
const canvas = document.getElementById("game")
let c = canvas.getContext("2d")
c.drawImage("https://heckapix.com/images/spaceship-clipart-with-background.jpg", 20, 20)
<canvas id="game"></canvas>
So is there a function to draw images from a link or is there a way to make the function work without making an image tag?
You can do it if you pre-load your remotely hosted image in an img tag, and hide it (style attribute):
<img id="my-image" src="https://heckapix.com/images/spaceship-clipart-with-background.jpg" style="display: none; visibility: hidden;">
Then use the DOM img in the context:
let img = document.getElementById('my-image');
c.drawImage(img, 20, 20)
I'm not sure I fully understand why you're using the image in the HTML (and using CSS to make it invisible), but you can always convert it to a Base64 string, and load it into the canvas using JavaScript:
let img = new Image();
img.src = 'data:image/png;base64, VBORw0KGgoAAAANSUhEUgAAAAUAAAAFCCNbyblAA....==';
c.drawImage(img);
I'm using html2canvas 1.0.0-alpha.9, and using jsplumbtoolkit to display Layout. I want save html on screen layout to image pnj file. But I have an bug when zoom in, zoom out html.
My code like this:
function PreviewPrinter() {
$("#previewImage").html('');
var cvs = document.getElementById('jsPlumb_2_1');
html2canvas(cvs).then(function (canvas) {
var myImage = canvas.toDataURL("image/png");
$("#previewImage").html("<img src=" + myImage + ">");
});
ShowModal("printView");
}
But I have an error like below:
When I zoom in with transform: scale(0.306108); and drag 1 image out of position, this image will missing when I append to Canvas.
I have image example below, fist image I drag out of position.
Image I want to save file .PNJ
Image error when append to canvas, you can see the first image is missing
I am using sketch.js http://intridea.github.io/sketch.js/ so users can create drawing in my app. Once they are finished drawing, and click save, I convert the drawing into a png image using this function:
convertCanvasToImage = (canvas) ->
image = new Image()
image.src = canvas.toDataURL("image/png")
image
And then I just append the image to the DOM.
But what if a user wants to edit the drawing afterwards? How can I take that image and add it to the canvas that sketch.js uses?
I tried the following with failed results. When you click the png image I run this code to try to append the image to canvas, but it does not seem to work at all. The canvas is in a modal and once it appears I run the following code to append the image to canvas, but no image appears.
template.$('#sketchModal').on 'shown.bs.modal', (e) ->
c = document.getElementById("sketchPad")
ctx = c.getContext("2d")
ctx.drawImage(e.target, 0, 0)
I haven't used sketch js before, but seeing this code I came with an idea
<div class="tools">
Marker
Eraser
</div>
<canvas id="tools_sketch" width="800" height="300" style="background: url(http://farm1.static.flickr.com/91/239595759_3c3626b24a_b.jpg) no-repeat center center;"></canvas>
<script type="text/javascript">
$(function() {
$('#tools_sketch').sketch({defaultColor: "#ff0"});
});
</script>
You don't want to append the image in canvas but rather add it as a background.
Try it and please inform me if it worked.
I have a HTML5 canvas element with a background image. User is allowed to draw on the image and then need to save the complete canvas element with the background. I'm using below code for saving part but it only gets the canvas drawing but not the background image. what could i do to get the background image also?
var canvas = document.getElementById('your_canvas');
var context = canvas.getContext('2d');
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL
// so it can be saved as an image
document.getElementById('canvasImg').src = dataURL;
Update:
My HTML
<div name='coords1' class='canvas-area input-xxlarge' disabled
placeholder='Shape Coordinates' id='imgDiv'
data-image-url='BackGround.jpg'></div>
I have the above div in my HTML page. then i dynamically create the canvas and draw on it. It's a bit lengthy code.
Don't use image on the div, draw the image on the canvas ..
use the following code to draw image on canvas,
var img=new Image();
img.src=/*image src*/;
var ctx=canvas.getContext("2d");
img.onload=function()
{
ctx.drawImage(img,x,y,width,height);
}
After image drawing completion at img.onload callback , allow user to draw on canvas...
Now save the canvas drawing....
To Get the Canvas with backgound Image
context.globalCompositeOperation="destination-over";
drawImage(yourBackgroundImage,0,0);
I am working on a project where I have to listen for paste event and save the image from clipboard to server and display it on canvas for further drawing.
What is working: get clipboard image and save it, displaying the image on canvas as background.
What is not working: resizing canvas so that whole image can be displayed. Also, while saving, I does not save the drawing on background image rather it only saves the drawing.
I tried
var newImg = document.getElementById('justimg');
newImg.src = data.showthis;
newImg.onload= function(){
curHeight = newImg.height;
curWidth = newImg.width;
alert(curWidth);
alert(curHeight);}
to get image attribute but its showing canvas attributes only.
<img id="justimg">
<canvas id="bearimage" ></canvas>
Also please suggest how to save canvas drawing with background image.
You need to get the canvas element and then change its width and height like this,
var newImg = document.getElementById('justimg');
newImg.src = data.showthis;
newImg.onload= function(){
var canvas = document.getElementById('bearimage');
canvas.height = newImg.height ;
canvas.width = newImg.width ;
alert(canvas.height);
alert(canvas.width);}
To save canvas image you first need to draw the image on it and then you can save it!
Here is a link to save image, http://www.nihilogic.dk/labs/canvas2image/