javascript - Can't get image with canvas.getObjects(); -


enter image description here why next function can't find image ( when check debugger may see other elements not image)

function getitembyname3(name) {      var object = null,           objects = canvas.getobjects();           objectscount=canvas.getobjects().length;      (var = 0, len = objectscount; < len; i++) {        if (objects[i].type && objects[i].type == "image"){             if (objects[i].id && objects[i].id===name) {               object = objects[i];              break;             }         }        }       return object;     }       function backgroundsavetojsonf(){                 ////____________________________________________savejsonf;     var backgroundimage=getitembyname3(document.getelementbyid("selectproject").value+"imageid");     var backgroundimagefordb=json.stringify(backgroundimage.toobject(['id','sendtoback','selectable'])); 

i uploading image canvas next code:

document.getelementbyid('imgloader').addeventlistener("change", function(e) {     var file = e.target.files[0];     var reader = new filereader();     reader.onload = function(f) {         var data = f.target.result;         fabric.image.fromurl(data, function(img) {             var oimg = img.set({                 left: 0,                 top: 0,                 angle: 00,                 width: canvas.width,                 height: canvas.height,                 id:document.getelementbyid("selectproject").value+"imageid"             });             canvas.setbackgroundimage(oimg).renderall();             var dataurl = canvas.todataurl({                 format: 'png',                 quality: 0.8             });         });     };     reader.readasdataurl(file);  }); 

thank you

demo

document.getelementbyid('imgloader').addeventlistener("change", function(e) {      var file = e.target.files[0];      var reader = new filereader();      reader.onload = function(f) {          var data = f.target.result;          fabric.image.fromurl(data, function(img) {              var oimg = img.set({                  left: 0,                  top: 0,                  angle: 00,                  width: canvas.width,                  height: canvas.height              });              //canvas.setbackgroundimage(oimg).renderall();              canvas.add(oimg);              console.log(canvas.getobjects());              var dataurl = canvas.todataurl({                  format: 'png',                  quality: 0.8              });          });      };      reader.readasdataurl(file);  });    var canvas = new fabric.canvas('c');  canvas.add(new fabric.rect({      left: 50,      top: 50,      height: 100,      width:100,      stroke: 'red',      fill: ''  }))  canvas.add(new fabric.text('1',{      left: 20,      top: 50,      stroke: 'red',      fill: ''  }))  canvas.renderall();
canvas{   border:2px dotted blue;  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.16/fabric.min.js"></script>  <input type="file" id="imgloader" accept="image/*"> <br>  <canvas id='c' width='400' height='400'></canvas>

its because setting uploaded image background image canvas.setbackgroundimage(oimg) not adding object canvas. if want in object list add object canvas.add(oimg). check demo.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -