gpt4 book ai didi

javascript - 如何将 SVG 文件拖放到 Fabricjs Canvas 上?

转载 作者:行者123 更新时间:2023-12-02 13:43:37 24 4
gpt4 key购买 nike

我正在尝试将 Canvas 外部的 svg img 拖放到 Canvas 本身,它确实适用于简单图像(png、jpg、gif),但不适用于 svg 文件,因为我是 FabricJS 的新手,所以我我想知道如何配置它以便它也可以与 SVG 一起使用。我不想从 SVG 创建 Fabric.Image 对象,我想将它们作为 Fabric.PathGroup 删除,以便它们保留矢量信息和编辑功能。

您可以通过此链接发现该项目:

http://jsfiddle.net/w8kkc/309/

HTML

<div id="images">
<img draggable="true" src="http://i.imgur.com/8rmMZI3.jpg" width="100" height="100">
<object draggable="true" type="image/svg+xml" data="http://fabricjs.com/assets/1.svg" width="100" height="100"></object>
</div>

<div id="canvas-container">
<canvas id="canvas" width="400" height="250"></canvas>
</div>
<小时/>

Javascript (FabricJS)

var canvas = new fabric.Canvas('canvas');

function handleDragStart(e) {
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
this.classList.add('img_dragging');
}

function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}

e.dataTransfer.dropEffect = 'copy'; // See the section on the DataTransfer object.
// NOTE: comment above refers to the article (see top) -natchiketa

return false;
}

function handleDragEnter(e) {
// this / e.target is the current hover target.
this.classList.add('over');
}

function handleDragLeave(e) {
this.classList.remove('over'); // this / e.target is previous target element.
}

function handleDrop(e) {
if (e.preventDefault) {
e.preventDefault();
}

if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}

var img = document.querySelector('#images img.img_dragging');

// console.log('event: ', e);

var newImage = new fabric.Image(img, {
width: img.width,
height: img.height,
// Set the center of the new object based on the event coordinates relative
// to the canvas container.
left: e.layerX,
top: e.layerY
});
canvas.add(newImage);

return false;
}

function handleDragEnd(e) {
// this/e.target is the source node.
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
}

if (Modernizr.draganddrop) {
// Browser supports HTML5 DnD.

// Bind the event listeners for the image elements
var images = document.querySelectorAll('#images img');
[].forEach.call(images, function (img) {
img.addEventListener('dragstart', handleDragStart, false);
img.addEventListener('dragend', handleDragEnd, false);
});
// Bind the event listeners for the canvas
var canvasContainer = document.getElementById('canvas-container');
canvasContainer.addEventListener('dragenter', handleDragEnter, false);
canvasContainer.addEventListener('dragover', handleDragOver, false);
canvasContainer.addEventListener('dragleave', handleDragLeave, false);
canvasContainer.addEventListener('drop', handleDrop, false);
} else {
// Replace with a fallback to a library solution.
alert("This browser doesn't support the HTML5 Drag and Drop API.");
}

关于如何接受将 SVG 拖放到 Canvas 中,有什么建议吗?

最佳答案

http://jsfiddle.net/w8kkc/336/更新了 fiddle

JavaScript

var canvas = new fabric.Canvas('canvas');
var currentlyDragging;

function handleDragStart(e) {
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
this.classList.add('img_dragging');
currentlyDragging = e.target;
}

function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}

e.dataTransfer.dropEffect = 'copy'; // See the section on the DataTransfer object.
// NOTE: comment above refers to the article (see top) -natchiketa

return false;
}

function handleDragEnter(e) {
// this / e.target is the current hover target.
this.classList.add('over');
}

function handleDragLeave(e) {
this.classList.remove('over'); // this / e.target is previous target element.
}

function handleDrop(e) {
if (e.preventDefault) {
e.preventDefault();
}

if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}



// console.log('event: ', e);
var ext = currentlyDragging.src.substr(-3);
if (ext === 'svg') {
fabric.loadSVGFromURL(currentlyDragging.src, function(objects, options) {
var svg = fabric.util.groupSVGElements(objects, options);
svg.left = e.layerX;
svg.top = e.layerY;
canvas.add(svg);
});
} else {
var newImage = new fabric.Image(currentlyDragging, {
width: currentlyDragging.width,
height: currentlyDragging.height,
// Set the center of the new object based on the event coordinates relative
// to the canvas container.
left: e.layerX,
top: e.layerY
});
canvas.add(newImage);
}
return false;
}

function handleDragEnd(e) {
// this/e.target is the source node.
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
}

if (Modernizr.draganddrop) {
// Browser supports HTML5 DnD.

// Bind the event listeners for the image elements
var images = document.querySelectorAll('#images img');
var objects = document.querySelectorAll('#images object');
[].forEach.call(images, function (img) {
img.addEventListener('dragstart', handleDragStart, false);
img.addEventListener('dragend', handleDragEnd, false);
});
[].forEach.call(objects, function (obj) {
obj.addEventListener('dragstart', handleDragStart, false);
obj.addEventListener('dragend', handleDragEnd, false);
});
// Bind the event listeners for the canvas
var canvasContainer = document.getElementById('canvas-container');
canvasContainer.addEventListener('dragenter', handleDragEnter, false);
canvasContainer.addEventListener('dragover', handleDragOver, false);
canvasContainer.addEventListener('dragleave', handleDragLeave, false);
canvasContainer.addEventListener('drop', handleDrop, false);
} else {
// Replace with a fallback to a library solution.
alert("This browser doesn't support the HTML5 Drag and Drop API.");
}

关于javascript - 如何将 SVG 文件拖放到 Fabricjs Canvas 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42767859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com