- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我正在做一个项目,我需要使用 fabric.js 将图像从 PC(用户系统)拖放到 html5 canvas,因为我得到了 div(作为下拉列表)到 canvas 的代码,但被 PC 击中了(用户系统) Canvas 任何人都可以帮助我如何进一步进行。
这是我到目前为止完成的代码
var js_c_drawing, activeObject = null;
$(document).ready(function () {
setDrawingCanvasCoords();
js_c_drawing = new fabric.Canvas('c_drawing');
js_c_drawing.calcOffset();
if (typeof fabric.instances != "undefined") fabric.instances.push(js_c_drawing);
});
function setDrawingCanvasCoords() {
var wHeight = $(window).height() - 100;
var wWidth = $(window).width() - 164;
var drawingStyle = 'border:4px solid gray;top:20px;position:relative;background-color:black;' + 'width:' + wWidth + 'px; height:' + wHeight + 'px';
$("#divDrawing").attr('style', drawingStyle);
}
function showToolMenu(shapeMenu) {
var divShapesId = 'divShapes';
var divElement = $('#' + divShapesId);
var ele = document.getElementById('a' + shapeMenu);
elePosition = findPos(ele);
document.getElementById(divShapesId).style.left = elePosition[0] + 'px';
document.getElementById(divShapesId).style.top = elePosition[1] + (ele.offsetHeight) + 'px';
document.getElementById(divShapesId).style.zIndex = 100;
divElement.show();
var url = baseurl + shapeMenu;
$(divElement).load(url);
}
function hideToolMenu() {
var divShapesId = 'divShapes';
var divElement = $('#' + divShapesId);
document.getElementById(divShapesId).style.zIndex = 20;
divElement.hide(2000);
}
function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
}
return [curleft, curtop];
}
我已经尝试使用 http://www.html5rocks.com/en/tutorials/file/dndfiles/这但它显示图像大小等,我已经尝试过http://jsfiddle.net/natchiketa/w8kkc/此代码与 PC(用户系统)和 Canvas 有关,但未成功。
最佳答案
终于找到了解决方案http://jsfiddle.net/rodrigopandini/gj7HT/使用以上两个链接。这是代码
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) {
// this / e.target is current target element.
/*
if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}
*/
e.stopPropagation(); // Stops some browsers from redirecting.
e.preventDefault(); // Stops some browsers from redirecting.
// handle desktop images
if(e.dataTransfer.files.length > 0){
var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (f.type.match('image.*')) {
// Read the File objects in this FileList.
var reader = new FileReader();
// listener for the onload event
reader.onload = function(evt) {
// create img element
var img = document.createElement('img');
img.src= evt.target.result;
// put image on canvas
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);
};
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
}
// handle browser images
else{
var img = document.querySelector('#images img.img_dragging');
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.");
}
关于drag-and-drop - 使用 fabric.js 将图像从 pc(用户系统)拖放到 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17312749/
我有一个带有 H264 身份验证的 RTSP 流的 URL - MPEG-4 第 10 部分编解码器。 Lik 看起来像这样:rtsp://login:pass@xxx.xxx.xxx.xxx:557
我正在开发移动应用程序。现在,我将访问 token 放在SharedPrefences上。我想问一下SharedPrefences上的存储访问 token 是否安全?如果没有,最好的做法是在哪里放置访
一个 UITableView,它有 20 行的 UITableViewCell,每一行都有一个按钮,单击按钮我想将 UITableViewCell 放到 UITableView 的底部。 是否有任何委
我有这样的代码。我删掉了所有的细节,但是这个文本区域会在按下按钮后发生变化,而它所做的只是改变了文本区域。没有添加滚动。我什至尝试将滚动策略设置为始终,但它返回错误。此面板将添加到另一个带有 Bord
我想在 NSString 中有 src="字符。 但我不能这样做,因为 "是 Objective C 的一部分。 我想这样做: NSString * name = [ [NSString alloc]
非常新手的问题,我是 html 的新手,特别是 highcharts 的东西。所以首先,我制作了这个 test.php 来显示图表。 Highcharts Exampl
如何将我的 fab 放在我的 fragment 上?请帮助我收到错误,例如。 android.view.InflateException: Binary XML file line #13: Erro
我已经完成了一个unity的迷你游戏,名为“penguins_test”。我导出了 2 个文件: penguins_test.html penguins_test.unity3d 现在,我想使用 Go
有谁知道如何将 uiviewcontroller(或只是 View )添加到 cocos2d 层上? 最佳答案 是这样的: [[CCDirector sharedDirector].view addS
我知道您可以采取一些技巧,将 HTML 元素放在 Silverlight 应用程序的顶部,但这无法全屏显示。有没有办法在 Silverlight 应用程序中显示 html 页面? 最佳答案 http:
从 Golang 教程中我不清楚如何将 Golang 代码放到 Github 以便以后能够从 Github 将该代码作为包导入。 这是来自 Golang 教程 http://golang.org/do
我围绕 Pygame 构建了我的整个游戏,并想把它放到 Steam 上。最后我了解到我需要 OpenGL 支持才能运行 Steam 的叠加层。初始化显示的代码: screen = pygame.dis
我需要将已编译的 apk 文件复制到作业 artifats,以便在成功时能够直接从作业页面下载它。 现在我已经在文件夹 build/apk/MyProject-release.apk 中编译了 apk
我愿意在本地和 Heroku 上处理不同的设置。所以我在 settings.py 的末尾使用了这个导入: try: from local_settings import * except Im
我是安卓新手。这个问题是我尝试使用 Android 应用程序和 Web 服务的步骤中的一个阶段。我之前问过一个问题,在这里:Fail to connect android to web service
我是 Git 新手。我刚刚在我的计算机上创建了一个 Git 存储库。现在我想将该存储库连接到网站 (GitHub) 并将我的代码推送到那里。 我该怎么做?我有点熟悉......我必须以某种方式将在线代
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How to use an existing database with an Android applic
如何在浏览器选项卡上显示标签名称? :( 帮我我想将我的标签显示为选项卡,有什么技巧吗? 技巧 - ABC 技巧 - 最佳答案 是的,您可以使用 在浏览器选项卡中显示标签名称 示例标题代码:
我需要 Tomcat 上的 JAX-WS 运行时。 把“jax-ws/lib/*”放到“tomcat/lib”里可以吗? 如果将 jax-ws/lib 放入 tomcat/lib,是否需要 Tomca
我已经在Win10系统中安装了hadoop-2.5.2和eclipse neon.1a,但是我把“hadoop-eclipse-plugin-2.5.2.jar”放到/eclipse/plugins后
我是一名优秀的程序员,十分优秀!