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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?