Note that there are some explanatory texts on larger screens.

plurals
  1. POError While passing File name from array in content.drawSvg
    primarykey
    data
    text
    <p>I am drawing SVG file on Canvas.</p> <p>For this I am using canvg Javascript SVG parser and renderer on Canvas.</p> <p>If I use : </p> <p>context.drawSvg('/static/images/awesome_tiger.svg', 0, 0, 400*scaleValue, 400*scaleValue);</p> <p>then it works fine.</p> <p>But if I use : </p> <p>context.drawSvg(images[0], 0, 0, 400*scaleValue, 400*scaleValue);</p> <p>then following error is coming.</p> <p><strong>Error : Uncaught TypeError: Object # has no method 'substr' in canvg.js</strong></p> <p>images = ["/static/images/awesome_tiger.svg"]</p> <p><strong>Original Code:</strong></p> <pre><code>var dataJSON = data || []; var dataJSON2 = data2 || []; window.onload = function(){ var canvas = document.getElementById("myCanvas"); var context = canvas.getContext('2d'); //tooltipTxt = ["Region 1", "Region 2", "Region 3"]; var tooltip = null, timer; console.log(dataJSON); var files = ["/static/images/awesome_tiger.svg", "/static/images/green1.png", "/static/images/pink.png"], images = [], numOfFiles = files.length, count = numOfFiles; // in files variable no use of svg name. //var x = canvg('myCanvas', '/static/images/awesome_tiger.svg') In this we can load svg with this method also but we used drawSvg method /// function to load all images in one go function loadImages() { /// go through array of file names for(var i = 0; i &lt; numOfFiles; i++) { /// create an image element var img = document.createElement('img'); /// use common loader as we need to count files img.onload = imageLoaded; //img.onerror = ... handle errors img.src = files[i]; /// push image onto array in the same order as file names images.push(img); } } loadImages(); function imageLoaded(e) { /// for each successful load we count down count--; //if (count === 0) if (count === 0) start(); //start when nothing more to count } function start() { context.clearRect(0, 0, canvas.width, canvas.height); context.drawSvg('/static/images/awesome_tiger.svg', 0, 0, 400*scaleValue, 400*scaleValue); for(var i = 0, pos; pos = dataJSON[i]; i++) { /// as we know the alpha ball has index 1 we use that here for images context.drawImage(images[1], pos.x, pos.y, 20/scaleValue, 20/scaleValue); } for(var i = 0, pos; pos = dataJSON2[i]; i++) { context.drawImage(images[2], pos.x, pos.y, 20/scaleValue, 20/scaleValue); } } function draw1(scaleValue){ context.save(); context.setTransform(1,0,0,1,0,0) context.globalAlpha = 0.5; context.clearRect(0, 0, canvas.width, canvas.height); context.restore(); context.save(); context.drawSvg('/static/images/awesome_tiger.svg', 0, 0, 400*scaleValue, 400*scaleValue) context.scale(scaleValue, scaleValue); for(var i = 0, pos; pos = dataJSON[i]; i++) { /// as we know the alpha ball has index 1 we use that here for images context.drawImage(images[1], pos.x, pos.y, 20/scaleValue, 20/scaleValue); } for(var i = 0, pos; pos = dataJSON2[i]; i++) { /// as we know the alpha ball has index 1 we use that here for images context.drawImage(images[2], pos.x, pos.y, 20/scaleValue, 20/scaleValue); } context.restore(); }; draw1(scaleValue); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload