how to load canvas in jquery not from image but background-image?

how to load canvas in jquery not from image but background-image?

Hi,

So here is my problem: I want to load canvas from the background image of a div
and not direct from an image and I fail.
Does some one know how to do that?


Here I give a link on a test page in my website in order you see the question better

build canvas from image src or background-image

you can see the first 10 images where it is OK (METHOD 1 classic canvas from img src)


at the bottom you see the 4 sprites background-image I fail to transform in CANVAS



I show the code I us'e for the first method I use for canvas from an image and that is OK:


  1. <img class="bigimage" id = "IMG1a" alt="" src="img/ET1_448.jpg" width="100"> 
  2. <img class="bigimage" id = "IMG2a" alt="" src="img/ET2_448.jpg" width="100"> 
  3. <img class="bigimage" id = "IMG3a" alt="" src="img/ET3_448.jpg" width="100"> 

  4. <div class="autocenter">
  5. <canvas id="myCanvas_OUT0" width="303" height="539"></canvas>
  6. </div>
and the JQUERY

  1. $(".bigimage").click( function() 
  2. img_srcout0="";
  3.  img_srcout0 = $(this).attr("id"); 
  4. elemout0 = document.getElementById("myCanvas_OUT0"); 
  5. if (elemout0 && elemout0.getContext) 
  6. contextout0 = elemout0.getContext("2d"); 
  7. if (contextout0) 
  8.       { 
  9.       imgout0=document.getElementById(img_srcout0); 
  10.       contextout0.drawImage(imgout0,0,0); 
  11.       } 
  12. imgdout0 = contextout0.getImageData(0, 0, 303,539); 
  13. pixout0 = imgdout0.data; 
  14. });

and that is fine.

But  for responsive reason I want to use backgroundimages  and here I fail to find a solution:


I try with backgound image in a sprite





Here is the code that I am trying to build
  1. <div id="id-menu" class="c-menu"> 
  2. SPRITE<br/> 
  3. <ul class="icone"> 
  4. <a href="#" id = "IMG1" ><li class="icone-1" id = "IMG1child" ></li></a> 
  5. <a href="#" id = "IMG2" ><li class="icone-2" id = "IMG2child" ></li></a> 
  6. <a href="#" id = "IMG3" ><li class="icone-3" id = "IMG3child" ></li></a> 
  7. <a href="#" id = "IMG4" ><li class="icone-4" id = "IMG4child" ></li></a> 
  8. </ul> 
  9.  </div>

with the JQUERY

  1. $("a").click( function() { 
  2. img_srcout0=""; 
  3. img_srcout0= $(this).children(":first").attr("id"); 
  4. alert(img_srcout0); //unitl here it seems ok
  5. elemout0 = document.getElementById("myCanvas_OUT0"); 
  6. if (elemout0 && elemout0.getContext) 
  7. contextout0 = elemout0.getContext("2d"); 
  8. if (contextout0) 
  9.       { 
  10.       imgout0=document.getElementById(img_srcout0); 
  11.       contextout0.drawImage(imgout0,0,0); 
  12.       } 
  13. imgdout0 = contextout0.getImageData(0, 0, 303,539); 
  14. pixout0 = imgdout0.data; 
  15. });

and the CSS

          
  1. .icone-1 { background-position: left top } 
  2. .icone-2 { background-position: 14.2857% top } 
  3. .icone-3 { background-position: 28.5714% top } 
  4. .icone-4 { background-position: 42.8571% top } 
  5. .icone-5 { background-position: 57.1428% top } 
  6. .icone-6 { background-position: 71.4285% top } 
  7. .icone-7 { background-position: 85.7142% top } 
  8. .icone-8 { background-position: right top } 
  9. ul { 
  10. padding:0px; 
  11. border:3px solid blue; 
  12. display: inline-block; 
  13. .icone li { 
  14. margin:auto; text-align:center;
  15. vertical-align:bottom; padding:0px; 
  16. width: 75px; 
  17. height: 100px; 
  18. border-radius:16px; 
  19. border: solid white 5px; 
  20. opacity: 1; 
  21. background-image: 
  22. url("http://www.davidinlove.com/img/sprite_menu_DT.jpg"); 
  23. background-repeat: no-repeat; 
  24. display: inline-block; 
  25. list-style-type:none; 
  26. }
Thanks if Some One knows how to do that.

For me it is important to use in a responsive way and for loading times of sprite
faster.

Loading a lot of images is long for tablet and using sprite with background images




defines in mediaqueries enable load images of different sizes for different devices (mobile table desktop)


Thanks in advance 



David  ( @webtecgeek   www.thecacaocafe.com )