Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - How to save two canvas(outer and inner) as png using fabric js in angular

enter image description here

while i save canvas it only saves outer canvas and ignores inner canvas

 //html

<button type="button" (click)="save()">save</button>
<div class="outercanvas">
      <canvas id="outercanvas"  width="500" height="500"></canvas>  
</div>
<div class="outerCanvas">
    <canvas id="outerCanvas" width="140" height="170" ></canvas>  
</div>

//ts code

canvas  = new fabric.Canvas('outercanvas');
canvas1 = new fabric.Canvas("innerCanvas");
save(){
 window.open(this.canvas.toDataURL('png'));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ofc because you never asked it to save the innercanvas just like you did for outercanvas.

save(){
 window.open(this.canvas.toDataURL('png'));
}

To save inner canvas as separately you can call same for inner canvas.

save(){
 window.open(this.canvas.toDataURL('png')); //outercanvas
 window.open(this.canvas1.toDataURL('png')); //innercanas
}

If you want to save both canvases in one image then follow below steps

  1. Take output of inner canvas
  2. Place that image over outercanvas using fabric.Image.fromURL()
  3. Save the output of outer canvas
  4. Remove the image from outercanvas, so it will be like, image never exist

Note: In 2nd step you have to position the image, use left and top property for or that, if you want to place it in center using canvas.centerObject(img);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...