i have created one canvas by using canvg plugin.
Then i want to put another background image below canvas using ctx.drawimage() [created by canvg].
I tried .
Please suggest.
var svgString = new XMLSerializer().serializeToString(document.querySelector('svg'));
var canvas = document.getElementById("canvas");
var DOMURL = self.URL || self.webkitURL || self;
ctx = canvas.getContext("2d");
canvg(canvas, svgString, { ignoreMouse: true, ignoreAnimation: true});
var img1 = new Image();
img1.onload = function() {
ctx.drawImage(img1, 136, 136);
var png = canvas.toDataURL("image/png");
document.querySelector('#chart').innerHTML = '<img src="'+png+'"/>';
DOMURL.revokeObjectURL(png);
};
img1.src= "chart.png";
This is working. Used ctx.drawSvg(svgString,0,0,500,500); instead of drawImage
var svgString = new XMLSerializer().serializeToString(document.querySelector('svg'));
var canvas = document.getElementById("canvas");
var DOMURL = self.URL || self.webkitURL || self;
var ctx = canvas.getContext("2d");
var DOMURL = self.URL || self.webkitURL || self;
var img = new Image();
//var svg = new Blob([svgString], {type: "image/svg+xml;charset=utf-8"});
var svg = new Blob([svgString], {type: "image/svg+xml"});
var url = DOMURL.createObjectURL(svg);
var png = '';
var img1 = new Image();
img1.onload = function(){
ctx.drawImage(img1, 136, 136);
ctx.drawSvg(svgString,0,0,500,500);
var png = canvas.toDataURL("image/png");
document.querySelector('#chart').innerHTML = '<img src="'+png+'"/>';
DOMURL.revokeObjectURL(png);
};
img1.src = 'chart.png';
Related
I am trying to invert the color of an image. Now the image loads ok, but when I try to call getImageData and putImageData back onto the canvas. The canvas is simply blank. Then I printed out all imageData, it appears like that it is always 0 for some reason. I am seriously troubled by this. Please help and thx in advance!
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = new Image();
img.src = "hand.jpg";
img.onload = function() {
ctx.drawImage(img, 10, 10,50,50);
}
var imgData = ctx.getImageData(10,10,50,50);
console.log(imgData.data.toString()); //return 0's
for (var i=0;i<imgData.data.length;i+=4)
{
imgData.data[i]=255-imgData.data[i];
imgData.data[i+1]=255-imgData.data[i+1];
imgData.data[i+2]=255-imgData.data[i+2];
imgData.data[i+3]=255;
}
ctx.putImageData(imgData,100,100);
}
You need to wait for the image to load
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = new Image();
img.src = "hand.jpg";
//================================================================
// this event will only get called after you have returned from this function
img.onload = function() {
ctx.drawImage(img, 10, 10,50,50);
}
//================================================================
// The image has not loaded at this point
// the following code will always be working on an empty canvas
var imgData = ctx.getImageData(10,10,50,50);
console.log(imgData.data.toString()); //return 0's
for (var i=0;i<imgData.data.length;i+=4)
{
imgData.data[i]=255-imgData.data[i];
imgData.data[i+1]=255-imgData.data[i+1];
imgData.data[i+2]=255-imgData.data[i+2];
imgData.data[i+3]=255;
}
ctx.putImageData(imgData,100,100);
//================================================================
// onload will fire after this point
}
To fix
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = new Image();
img.src = "hand.jpg";
img.onload = function() {
ctx.drawImage(img, 10, 10,50,50);
var imgData = ctx.getImageData(10,10,50,50);
for (var i=0;i<imgData.data.length;i+=4){
imgData.data[i]=255-imgData.data[i];
imgData.data[i+1]=255-imgData.data[i+1];
imgData.data[i+2]=255-imgData.data[i+2];
imgData.data[i+3]=255;
}
ctx.putImageData(imgData,100,100);
}
}
BTW you can invert the image colours with
ctx.globalCompositeOperation = "difference";
ctx.fillStyle = "white"
ctx.fillRect(0,0,ctx.canvas.width,ctx.canvas.height);
ctx.globalCompositeOperation = "source-over"; // restore default
If i try launch code in localhost. This not showing image with mozilla firefox but in other browsers show successfully. If i try load little image load successfully
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<canvas id="containerScaled"></canvas>
<script>
$(document).ready(function() {
var canvasScaled = document.getElementById("containerScaled");
var ctxScaled = canvasScaled.getContext("2d");
var imageWidth = 500;
var imageHeight = 500;
img = new Image();
img.onload = function() {
ctxScaled.drawImage(img, 0, 0, imageWidth, imageHeight);
};
img.src = 'http://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg'
});
</script>
Here is how it looks in this context:
$(document).ready(function() {
var canvasScaled = document.getElementById("containerScaled");
var ctxScaled = canvasScaled.getContext("2d");
var imageWidth = 500;
var imageHeight = 500;
img = new Image();
img.onload = function() {
ctxScaled.drawImage(img, 0, 0, imageWidth, imageHeight);
};
img.src = 'http://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="containerScaled"></canvas>
I am a new user and cannot comment on this post: dragging and resizing an image on html5 canvas regarding drag and drop/resize image with anchors...
could someone please clarify how to apply the resize/drag/anchors to an uploaded image rather than a pre-loaded image please?
I have had a go but its killing me..
uploader html:
<input type="file" id="imageLoader" name="imageLoader" />
uploader js:
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
function handleImage(e) {
var reader = new FileReader();
reader.onload = function (event) {
var img = new Image();
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, 200, 140);
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
fiddle
your code is written to handle the global var img.
When you load a new image, a new local img var is created, which you only draw on the context.
What you need is to have the new image replace the old one.
So just change, in your file loader :
function handleImage(e) {
var reader = new FileReader();
reader.onload = function (event) {
img = new Image(); // not 'var img=...'
img.onload = function () {
draw(true,true); // just update your app
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
it workd, as you can see here :
http://jsfiddle.net/LAS8L/108/
I am using Google Maps API V3 for showing Google map. I created a custom control for printing the map. But when I'm printing it the marker is not in that print out. Can anyone tell me how do I print the map along with the marker ? Thanks in Advance.
EDIT
Code to create the map
function map_start(el,id){
$lat = $("#"+id).attr("lat");
$lang = $("#"+id).attr("long");
var image = "../assets/paleblue_MarkerU.png";
var myOptions = {
center: new google.maps.LatLng($lat,$lang),
zoom: 12,
disableDefaultUI: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$map = new google.maps.Map(document.getElementById('inset'),myOptions);
var placeMarker = new google.maps.Marker({
position: new google.maps.LatLng($lat,$lang),
map: $map,
animation: google.maps.Animation.DROP,
icon: image
});
var homeControlDiv = document.createElement('div');
var homeControl = new HomeControl(homeControlDiv, $map);
homeControlDiv.index = 1;
$map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
}
Code for custom control to print the map
function HomeControl(controlDiv, map) {
controlDiv.style.padding = '5px';
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = 'white';
controlUI.style.borderStyle = 'solid';
controlUI.style.borderWidth = '2px';
controlUI.style.cursor = 'pointer';
controlUI.style.textAlign = 'center';
controlUI.title = 'Click to print the map';
controlDiv.appendChild(controlUI);
var controlText = document.createElement('div');
controlText.style.fontFamily = 'Arial,sans-serif';
controlText.style.fontSize = '12px';
controlText.style.paddingLeft = '4px';
controlText.style.paddingRight = '4px';
controlText.className = "gmnoprint";
controlText.innerHTML = '<b>Print</b>';
controlUI.appendChild(controlText);
google.maps.event.addDomListener(controlUI, 'click', function() {
printDiv('cboxContent')
});
}
Code for printing the map
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
window.location.reload();
}
I have a problem when I use drawImage function to draw an image inside an html5 canvas ;used with a code Sencha Touch 2 images look blu and they don't have height quality this is the code:
//We've added a third and final item to our tab panel - scroll down to see it
var index=0;
Ext.application({
name: 'Sencha',
launch: function() {
var panel = Ext.create("Ext.Panel", {
fullscreen: true,
items: [
{
title: 'Home',
iconCls: 'home',
cls: 'home',
width: '533px',
listeners: {
painted: function() {
var a={Source:'images/2.tiff',Titre:'T1',Description:'D1'};
var b={Source:'images/Im2.jpg',Titre:'T1',Description:'D1'};
var c={Source:'images/Im3.jpg',Titre:'T1',Description:'D1'};
var d={Source:'images/Im4.jpg',Titre:'T1',Description:'D1'};
var e={Source:'images/Im5.jpg',Titre:'T1',Description:'D1'};
var f={Source:'images/Im6.jpg',Titre:'T1',Description:'D1'};
var img = new Image();
var img1 = new Image();
var img2 = new Image();
img.src = "images/Im1.jpg";
img1.src = "images/Im2.jpg";
img2.src = "images/Im3.jpg";
var ImageP = new Image();
//********************
var x,y,Dim_Image,espacement;
Dim_Image=50;
espacement=30;
var Presentation=new Array();
Presentation[0]=a;
Presentation[1]=b;
Presentation[2]=c;
Presentation[3]=d;
Presentation[4]=e;
Presentation[5]=f;
//********************
//°°°°°°°°°°°°°°°°°°°°°°
/* Ici on teste le tableau*/
var testImage = new Array();
//cw-(i*espacement),i*espacement,Dim_Image,Dim_Image);
var j=0;
for(i=index;i<index+3;i++)
{
testImage[i]= new Image();
testImage[i].src=Presentation[i].Source;
testImage[i].onload = function()
{
context.drawImage(testImage[j],cw-80-(j*espacement),20+j*espacement,Dim_Image,Dim_Image);
j++;
}
}index = index +1;
var img = new Image();
var canvas = document.getElementById('mycanvas');
var context = canvas.getContext("2d");
var cw = canvas.width;
var ch = canvas.height;
//Event================================================================
canvas.addEventListener("mousedown" ,function(event){
testImage = new Array();
context.clearRect(0,0,300,300); // clear canvas
var j=0;var inf =index;var ss;
for(i=index;i<index+3;i++)
{
testImage[i-index]= new Image();
testImage[i-index].src=Presentation[i].Source;
if( i == index+2)
{alert("index +3");
testImage[i-index].onload = function()
{context.globalAlpha=1;
context.drawImage(testImage[j],cw-80-(j*espacement),20+j*espacement,Dim_Image,Dim_Image);
j++;
}
}
else{
testImage[i-index].onload = function()
{context.globalAlpha=0.5;
context.drawImage(testImage[j],cw-80-(j*espacement),20+j*espacement,Dim_Image,Dim_Image);
j++;
}
}
}
index=index+1;
context.stroke();
},false);
context.strokeStyle = 'black';
context.stroke();
//};
},
scope: this
},
html: [
' <canvas id="mycanvas" style="background-color:grey; width:100%; height:100%;">no canvas support</canvas>'
]
} //this is the new item
]
});
}
});
What exactly you are trying to achieve?
Perhaps the problem is related to the drawImage method type you are using
Have a look at this link on the usage how to use, and what exactly the parameters that you added means.
Problem may be aspect ratio , width , height calculation from your side.
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage