我是 HTML5 新手,正在尝试使用 JCrop 裁剪图像。如果我想直接裁剪它是没有问题的,但是当它被绘制到画布中时 JCrop 不起作用。
我认为原因可能是我正在创建一个图像变量,以便能够将其绘制到视口画布中并动态设置它的 Id 。所以 Jquery 无法访问动态创建的图像。但即便如此我也不知道该怎么办。
这是我的完整 HTML 代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>acanimal - Crop image on the client side with JCrop and HTML5 canvas element</title>
<script src="./js/jquery.min.js" type="text/javascript"></script>
<script src="./js/jquery.Jcrop.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="./css/jquery.Jcrop.css" type="text/css" />
<script type="text/javascript">
$(window).load(function () {
var canvas = document.getElementById('viewport'),
context = canvas.getContext('2d');
make_base();
function make_base() {
var base_image = new Image();
base_image.id = 'target';
base_image.src = 'demo_files/sago.jpg';
base_image.onload = function () {
context.drawImage(base_image, 0, 0);
}
}
});
jQuery(function ($) {
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
allowSelect: true,
allowMove: true,
allowResize: true,
aspectRatio: 0
});
function updatePreview(c) {
if (parseInt(c.w) > 0) {
// Show image preview
var imageObj = $("#target")[0];
var canvas = $("#preview")[0];
var context = canvas.getContext("2d");
if (imageObj != null && c.x != 0 && c.y != 0 && c.w != 0 && c.h != 0) {
context.drawImage(imageObj, c.x, c.y, c.w, c.h, 0, 0, canvas.width, canvas.height);
}
}
};
});
</script>
</head>
<body>
<canvas id="viewport" width=602; height=500;></canvas>
<canvas id="preview" style="width:150px;height:150px;overflow:hidden;"></canvas>
</body>
</html>
我究竟做错了什么?
谢谢...
你已经设置好了Jcrop
不正确,试试吧。
function make_base() {
var base_image = new Image();
base_image.src = 'https://picsum.photos/id/870/700/467';
base_image.onload = function() {
context.drawImage(base_image, 0, 0);
}
}
function updatePreview(c) {
if (parseInt(c.w) > 0) {
// Show image preview
var imageObj = $("#viewport")[0];
var canvas = $("#preview")[0];
var context = canvas.getContext("2d");
if (imageObj != null && c.x != 0 && c.y != 0 && c.w != 0 && c.h != 0) {
context.drawImage(imageObj, c.x, c.y, c.w, c.h, 0, 0, canvas.width, canvas.height);
}
}
}
var canvas = document.getElementById('viewport'),
context = canvas.getContext('2d');
make_base();
$('#viewport').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
allowSelect: true,
allowMove: true,
allowResize: true,
aspectRatio: 0
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/tapmodo/[email protected] /cdn-cgi/l/email-protection/js/jquery.Jcrop.min.js"></script>
<link href="https://cdn.jsdelivr.net/gh/tapmodo/[email protected] /cdn-cgi/l/email-protection/css/jquery.Jcrop.min.css" rel="stylesheet" />
<canvas id="viewport" width="602" height="500"></canvas>
<canvas id="preview" style="width:150px;height:150px;overflow:hidden;"></canvas>
在 JSFiddle 上查看 http://jsfiddle.net/wr2er0Lj/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)