我如何使用HTML5画布和JavaScript裁剪图像,以便离线裁剪图像
How can i crop image using the HTML5 canvas and JavaScript in order to crop an image offline?
互联网上有几个工具可以使用JavaScript和PHP裁剪图像,但不幸的是,如果我们打算让我们的应用程序严格脱机,那么我们就没有服务器端PHP脚本可以依赖,所以要实现这一点,我们必须使用HTML5画布和JavaScript来脱机裁剪图像
如果图像起源于本地域,那么您可以轻松地使用html画布裁剪它
但是,如果图像来自另一个域,您将遇到CORS安全错误:http://code.google.com/p/html5security/wiki/CrossOriginRequestSecurity
如果需要,你也可以在裁剪时放大/缩小。
下面是使用canvas' drawImage
裁剪图像的示例代码:
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var img=new Image();
img.onload=function(){
crop();
}
img.src=document.getElementById("source").src;
function crop(){
// this takes a 105x105px crop from img at x=149/y=4
// and copies that crop to the canvas
ctx.drawImage(img,149,4,105,105,0,0,105,105);
// this uses the canvas as the src for the cropped img element
document.getElementById("cropped").src=canvas.toDataURL();
}
}); // end $(function(){});
</script>
</head>
<body>
<img id="source" width=400 height=234 src="localImage.png">
<img id="cropped" width=105 height=105>
<canvas id="canvas" width=105 height=105></canvas>
</body>
</html>
相关文章:
- 如何从缩放的图像裁剪并保持原始图像的纵横比
- 如何使用Javascript图像裁剪器“Croppie”保存
- Javascript 中的图像裁剪和转换 - 扫描应用程序
- 使用绘制图像裁剪在 Safari 中不起作用
- Javascript图像裁剪画布错误
- HTML 图像裁剪 + 显示图像代码
- 在HTML5画布中将图像裁剪为椭圆形
- 搜索js图像裁剪插件,无需jquery
- 图像裁剪和结合水印在网站上
- Html5, js和css3图像裁剪工具
- 在jQuery中移动图像(图像裁剪)
- 是否有类似于Facebook's图像裁剪的jQuery图像裁剪插件?
- Darkroomjs图像裁剪后到PHP
- 我已经使用html画布实现了javascript图像裁剪功能.它在chrome和IE上运行得很好(?!),但在firef
- 如何使用fengyuanchen/cropper裁剪图像,并在数据库中保存图像裁剪的路径
- 自动图像裁剪
- Wordpress图像裁剪器JS错误
- HTML5画布;图像裁剪和多个图像
- 图像裁剪模块
- jQuery,画布图像裁剪-索引或大小为负或大于允许的数量