我已经使用html画布实现了javascript图像裁剪功能.它在chrome和IE上运行得很好(?!),但在firef
I have implemented a javascript image cropping feature using html canvas. It is working perfectly on chrome and IE(?!), but not on firefox
https://jsfiddle.net/1gf6xzyz/3/是小提琴的链接。我将指出JS代码中的重要点以及正在尝试的内容。
第22行:这是我将cropper附加到图像对象的地方。cropper是一个类,它引用图像和具有不言自明名称的选项。重要的是:
- ratioDim—裁剪窗口的纵横比
-
maxCaptureWidth-可以裁剪的图像的最大宽度。
cropper = new Cropper.Img('imgSelector', { minWidth : 120, minHeight : 90, ratioDim : { x : 120, y : 90 }, displayOnInit : true, imageDataCallback : imageDataCallback, maxCaptureWidth : 400, })
作物库从第47行开始。我对现有的图书馆进行了改造以满足我的需要。
第357行-这是在图像加载后将cropper连接到图像的位置。所发生的转换列为从358到386。
onLoad : function() {
/*
* Build the container and all related elements, will result in
* the following
*
* <div class="imgCrop_wrap"> <img ... this.img ... /> <div
* class="imgCrop_dragArea"> <!-- the inner spans are only
* required for IE to stop it making the divs 1px high/wide -->
* <div class="imgCrop_overlay imageCrop_north"><span></span></div>
* <div class="imgCrop_overlay imageCrop_east"><span></span></div>
* <div class="imgCrop_overlay imageCrop_south"><span></span></div>
* <div class="imgCrop_overlay imageCrop_west"><span></span></div>
* <div class="imgCrop_selArea"> <!-- marquees --> <!-- the
* inner spans are only required for IE to stop it making the
* divs 1px high/wide --> <div class="imgCrop_marqueeHoriz
* imgCrop_marqueeNorth"><span></span></div> <div
* class="imgCrop_marqueeVert imgCrop_marqueeEast"><span></span></div>
* <div class="imgCrop_marqueeHoriz imgCrop_marqueeSouth"><span></span></div>
* <div class="imgCrop_marqueeVert imgCrop_marqueeWest"><span></span></div>
* <!-- handles --> <div class="imgCrop_handle imgCrop_handleN"></div>
* <div class="imgCrop_handle imgCrop_handleNE"></div> <div
* class="imgCrop_handle imgCrop_handleE"></div> <div
* class="imgCrop_handle imgCrop_handleSE"></div> <div
* class="imgCrop_handle imgCrop_handleS"></div> <div
* class="imgCrop_handle imgCrop_handleSW"></div> <div
* class="imgCrop_handle imgCrop_handleW"></div> <div
* class="imgCrop_handle imgCrop_handleNW"></div> <div
* class="imgCrop_clickArea"></div> </div> <div
* class="imgCrop_clickArea"></div> </div> </div>
*/
.......
}
第574行-此函数的初始部分试图根据提供的"maxCaptureWidth"设置图像的最大尺寸。函数的其余部分计算裁剪区域相对于图像的左上和右下坐标。
var ratioOfContainer = this.img.naturalHeight
/ this.img.naturalWidth;
var baseWidth = (this.img.naturalWidth > this.options.maxCaptureWidth) ? this.options.maxCaptureWidth
: this.img.naturalWidth;
var corelatedHeight = baseWidth * ratioOfContainer;
this.img.width = baseWidth;
this.img.height = corelatedHeight;
this.imgW = baseWidth;
this.imgH = corelatedHeight;
第656行到第658行-这些是负责将图像捕获到画布上的主要功能。selArea是将绘制到画布上的区域。
this.selArea.show();
this.drawArea();
this.endCrop();
第758行-实用程序,以获取作物的宽度。
calcW : function() {
return (this.areaCoords.x2 - this.areaCoords.x1);
}
第768行-实用程序,以获取农作物的高度。
calcH : function() {
return (this.areaCoords.y2 - this.areaCoords.y1);
}
第1170行-绘图区域是保证在调整或移动cropper时调用的函数。它负责根据当前裁剪器位置在图像覆盖上应用样式。
1230行-绘制到画布的功能从这里开始。这部分在IE和Chrome中都能很好地工作。在firefox中,只有当左上角的cropper与左上角图像重合时,它才起作用。并且,挡板的垂直或水平移动会使捕获的图像失真。我已经彻底检查了传递给上下文drawinimage函数的参数,没有发现任何问题。
var ratioDimHeight = this.img.naturalHeight / this.img.height;
var ratioDimWidth = this.img.naturalWidth / this.img.width;
var canvas = document.createElement("canvas");
canvas.width = areaWidth;
canvas.height = areaHeight;
var context = canvas.getContext("2d");
context
.drawImage(
this.img,
this.areaCoords.x1 * ratioDimWidth,
this.areaCoords.y1 * ratioDimHeight,
this.img.width * ratioDimWidth,
this.img.height * ratioDimHeight,
0, 0, this.img.width,
this.img.height);
this.imageData = canvas.toDataURL('image/png');
第1252行-我将裁剪后的图像提取为dataurl,然后使用ajax将其发送到服务器端,在表单submit上提交并将图像字节上传到s3。
我不知道为什么它不能与firefox一起工作,我将非常感谢任何帮助。提前感谢您的考虑。
对我有效的更改如下:
this.img.width = this.img.width - this.areaCoords.x1;
this.img.height = this.img.height - this.areaCoords.y1;
context
.drawImage(
this.img,
this.areaCoords.x1 * ratioDimWidth,
this.areaCoords.y1 * ratioDimHeight,
this.img.width * ratioDimWidth,
this.img.height * ratioDimHeight,
0, 0, this.img.width * ratioDimWidth,
this.img.height * ratioDimHeight);
this.imageData = canvas.toDataURL('image/png');
this.img.width = this.img.width + this.areaCoords.x1;
this.img.height = this.img.height + this.areaCoords.y1;
我必须根据裁剪窗口的尺寸来缩小图像的尺寸。不知何故,firefox将画布捕获与图像维度相关联,这与其他浏览器不同。我希望我能说我用某种巧妙的方式解决了这个问题,但:(我玩了drawinimage函数,观察了它的行为,得出了这个"破解"。也许它对某人有用。
再次感谢DEfusion的整洁裁剪库。
- Jquery没有在firefox浏览器中调用Webservice方法,它在Chrome和IE中运行良好
- 在没有关闭H/W加速的情况下优化CSS3动画会让它在GPU上运行得更快吗
- 如何将word文档中的值作为输入传递到javascript的文本框中,并在循环中运行它
- 当屏幕大小为中或小或xs时,文本溢出,会弄乱整个表.不过,它在全屏模式下运行良好
- jQuery:防止绑定的 js 在单击时运行,然后在满足条件时运行它
- Javascript和Heroku.脚本未在服务器上运行,即使它在本地运行
- 我有AJAX,它在Firefox中运行良好,但在Chrome中不产生任何输出
- 如果我在控制台中运行它,为什么会出现语法错误?{} === {}.
- javascript的新手,我将如何嵌入我的javascript文件并在HTML字段中运行它
- onclick 事件 - 为什么 Javascript 在加载上运行它
- 清除运行时在它自己的函数中的超时
- 我可以将javascript存储在本地存储对象中并运行它吗
- 在函数中运行代码,就好像它在另一个作用域中运行一样
- 模板html/javascript/css的iphone.需要在UIWebView中运行它
- 我可以加密一个javascript文件,仍然让它在客户端上运行吗?
- Chrome不运行我的D3.js,但它在safari中工作良好
- 我的脚本运行完美,直到我包装它在这个if(屏幕).宽度& lt;980){}语句检查屏幕大小
- 返回页首滚动jquery在chrome中不起作用.为什么?它在firefox中运行良好
- 如何组合两个相似的脚本,并在略有变化的情况下运行它
- 如何在浏览器中修改JavaScript,然后在修改后运行它?