【发布时间】:2014-02-17 21:35:05
【问题描述】:
这是我的问题。
该代码适用于我的 android,但不适用于我的 iPhone 4。
在我选择要上传的图像后,图像预览大约是 50 像素高和 640 像素宽。我对每个计算操作都使用了警报,我可以看到数字是正确的。但是最后显示的图像非常错误。
有人知道发生了什么吗?
HTML:
<div id="files">
<input type="file" id="imageLoader" name="imageLoader" />
</div>
<div id="cont">
<canvas id="canvas"></canvas>
</div>
<button type="button" id="rotate" >Rotér 90°</button>
<input type="button" id="saveButton" value="LAGRE" onclick="SavePost()" />
JS:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var imgWidth = 640;
var imgHeight = 480;
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
var size = {
width: imgWidth,
height: imgHeight
};
var rotation = 0;
var deg2Rad = Math.PI / 180;
var img;
var fileName = "";
var img;
function handleImage(e) {
var reader = new FileReader();
reader.onload = function (event) {
img = new Image;
img.onload = draw;
img.src = event.target.result;
imgWidth = img.width;
imgHeight = img.height;
size = {
width: imgWidth,
height: imgHeight
};
};
reader.readAsDataURL(e.target.files[0]);
fileName = e.target.files[0].name;
}
function draw() {
var maxWidth = 640;
var scale = 1.00;
if (size.width > maxWidth) {
scale = maxWidth / size.width;
}
canvas.width = size.width * scale;
canvas.height = size.height * scale;
// calculate the centerpoint of the canvas
var cx = canvas.width / 2;
var cy = canvas.height / 2;
// draw the rect in the center of the newly sized canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(cx, cy);
ctx.rotate(rotation * deg2Rad);
ctx.scale(scale, scale);
ctx.drawImage(img, -imgWidth / 2, -imgHeight / 2);
ctx.restore();
}
document.getElementById("rotate").addEventListener("click", resizeClicked, false);
function resizeClicked(e) {
rotation += 90;
newSize(imgWidth, imgHeight, rotation);
draw();
}
function newSize(w, h, a) {
var rads = a * Math.PI / 180;
var c = Math.abs(Math.cos(rads));
var s = Math.abs(Math.sin(rads));
size.width = h * s + w * c;
size.height = h * c + w * s;
}
function SavePost() {
var canvas = $('canvas')[0];
var dataURL = canvas.toDataURL();
console.log(dataURL);
}
【问题讨论】:
-
我似乎在 iphone 5 上遇到了同样的问题。它只发生在某些手机上(无论版本如何 (4/5))。
标签: iphone html canvas html5-canvas