亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從導入的 SVG 設置畫布的高度和寬度

如何從導入的 SVG 設置畫布的高度和寬度

阿晨1998 2023-10-24 17:27:36
我想知道如何使用導入的 svg 的寬度/高度來設置 Fabricjs 的畫布的寬度/高度這是一個例子:const svg_data = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1500" height="800" viewBox="0 0 1500 800" xml:space="preserve"><desc>Created with Fabric.js 3.6.3</desc><defs></defs><rect x="0" y="0" width="100%" height="100%" fill="rgb(255, 255, 255)"></rect><g transform="matrix(1 0 0 1 50 50)"  ><rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" /></g><g transform="matrix(1 0 0 1 50 50)"  ><rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" /></g><g transform="matrix(1 0 0 1 50 50)"  ><rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" /></g><g transform="matrix(1 0 0 1 50 50)"  ><rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" /></g><g transform="matrix(1 0 0 1 525.5 25.5)"  ><rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;"  x="-475" y="-25" rx="0" ry="0" width="950" height="50" /></g>
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

只需使用選項中獲得的寬度/高度:


this.fabric.loadSVGFromString(svg_data, (objects, options) => {

  ...

  canvas.setHeight(options.height);

  canvas.setWidth(options.width);

  ...

});

const svg_data = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1500" height="800" viewBox="0 0 1500 800" xml:space="preserve">

<desc>Created with Fabric.js 3.6.3</desc>

<defs>

</defs>

<rect x="0" y="0" width="100%" height="100%" fill="rgb(255, 255, 255)"></rect>

<g transform="matrix(1 0 0 1 50 50)"  >

<rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" />

</g>

<g transform="matrix(1 0 0 1 50 50)"  >

<rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" />

</g>

<g transform="matrix(1 0 0 1 50 50)"  >

<rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" />

</g>

<g transform="matrix(1 0 0 1 50 50)"  >

<rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;"  x="-50" y="-50" rx="0" ry="0" width="100" height="100" />

</g>

<g transform="matrix(1 0 0 1 525.5 25.5)"  >

<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;"  x="-475" y="-25" rx="0" ry="0" width="950" height="50" />

</g>

<g transform="matrix(1 0 0 1 25.5 75.5)"  >

<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;"  x="-25" y="-75" rx="0" ry="0" width="50" height="150" />

</g>

<g transform="matrix(1 0 0 1 1425.5 175.5)"  >

<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;"  x="-25" y="-75" rx="0" ry="0" width="50" height="150" />

</g>

</svg>`


const canvas = new fabric.Canvas("canvas");


this.fabric.loadSVGFromString(svg_data, (objects, options) => {

  canvas.setHeight(options.height);

  canvas.setWidth(options.width);

  canvas.add.apply(canvas, objects);

  canvas.renderAll();

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"></script>


<div>

  <canvas id="canvas" style="border: 3px solid black;"></canvas>

</div>


查看完整回答
反對 回復 2023-10-24
  • 1 回答
  • 0 關注
  • 247 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號