douyan6871 2018-06-25 05:42
浏览 394
已采纳

Fabricjs:显示横幅的预览

I am using fabricjs 1.5 and I am stuck on 1 thing. I want to show the preview of the canvas to the user on click of button and I can not come up with a proper solution. Some things that crossed my mind are:

  • Save the current canvas state and then render it on the other canvas

  • Create a temporary image save function and then show this image

But I want to know if there is any specific function in the fabricjs that can help me achieve this. I did some R&D and could not find anything and hence I have not tried anything.

  • 写回答

1条回答 默认 最新

  • duanji9481 2018-06-25 10:31
    关注

    Use canvas.toDataURL() to get the image of canvas, and set it to image source.

    DEMO

    var canvas = new fabric.Canvas('c');
    canvas.add(new fabric.Circle({radius:100,fill:'red'}))
    function setPreview(){
     document.getElementById('img').src = canvas.toDataURL();
    }
    <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
    <button onclick="setPreview()">Preview</button><br>
    <canvas id="c" width="200" height="200" style="border:1px solid #ccc"></canvas>
    <br>
    <img id='img'/>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信