gpt4 book ai didi

HTML Canvas 在变换和旋转后重置绘图点

转载 作者:行者123 更新时间:2023-12-05 08:42:06 27 4
gpt4 key购买 nike

我有这个简单的 canvas 网页,它允许用户使用 HTML 输入类型文件 从相机上传照片。这个想法是让用户在他们的图像上自由绘图。但是,我有一个问题。

在某些设备上,来自相机的图像以错误的方向绘制到 Canvas 上,因此我必须为用户提供一个按钮来旋转他们的图像以获得正确方向的绘图。

问题是 Canvas 经过变换和旋转以获得正确的方向后,绘图坐标似乎偏离了方向。例如,如果我绘制直线水平线,我会在图像旋转一次后得到直线垂直线。我认为问题在于 Canvas 方向发生了变化。

那么图像经过变换和旋转后,如何修正绘图坐标呢?我的代码在下面..

window.onload = init;
var canvas, ctx, file, fileURL;
var mousePressed = false;
var lastX, lastY;

function init(){
canvas = document.getElementById('myCanvas')
ctx = canvas.getContext('2d')
canvas.addEventListener('mousedown', touchstartHandler, false)
canvas.addEventListener('mousemove', touchmoveHandler, false)
canvas.addEventListener('mouseup', touchendHandler, false)
canvas.addEventListener('mouseleave', touchcancelHandler, false)
}

function touchstartHandler(e){
e.preventDefault()
mousePressed = true;
Draw(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, false);
}
function touchmoveHandler(e){
e.preventDefault()
if (mousePressed) {
Draw(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
}
}

function touchendHandler(e){
e.preventDefault()
if (mousePressed) {
mousePressed = false;
}
}

function touchcancelHandler(e){
e.preventDefault()
if (mousePressed) {
mousePressed = false;
}
}

function Draw(x, y, isDown) {
if (isDown) {
ctx.beginPath();
ctx.strokeStyle = "blue";
ctx.lineWidth = 12;
ctx.lineJoin = "round";
ctx.moveTo(lastX, lastY);
ctx.lineTo(x, y);
ctx.closePath();
ctx.stroke();
}
lastX = x;
lastY = y;
}
<!DOCTYPE html>
<html>
<head>
<title>Portrait</title>
</head>
<body>
<canvas id="myCanvas"></canvas><br/>
<input type="file" onchange="fileUpload(this.files)" id="file-input" capture="camera"><br/><br/>
<button onclick="rotate()">Rotate</button>

<script>
var file, canvas, ctx, image, fileURL;
function fileUpload(files){
file = files[0]
fileURL = URL.createObjectURL(file)
canvas = document.getElementById('myCanvas')
canvas.style.backgroundColor = "blue"
ctx = canvas.getContext('2d')
image = new Image()

image.onload = function() {
canvas.width = 500
canvas.height = (500*this.height)/this.width
ctx.drawImage(image,0,0,canvas.width,canvas.height)
ctx.save();
}
image.src = fileURL
}

function rotate(){
ctx.clearRect(0,0,canvas.width,canvas.height)
ctx.translate(canvas.width/2, canvas.height/2)
ctx.rotate(90*Math.PI/180)
ctx.translate(-canvas.width/2, -canvas.height/2)
ctx.drawImage(image,0,0,canvas.width,canvas.height)
}
</script>
</body>
</html>

最佳答案

您需要save旋转和平移前的 Canvas 状态,然后是 restore转换完成时的状态。

var file, canvas, ctx, image, fileURL, rotation = 90;

function fileUpload(files) {
file = files[0]
fileURL = URL.createObjectURL(file)
canvas = document.getElementById('myCanvas')
canvas.style.backgroundColor = "blue"
ctx = canvas.getContext('2d')
image = new Image()

image.onload = function() {
canvas.width = 500
canvas.height = (500 * this.height) / this.width
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
}
image.src = fileURL
}

function rotate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save(); //save canvas state
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(rotation * Math.PI / 180);
ctx.translate(-canvas.width / 2, -canvas.height / 2);
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
rotation += 90;
ctx.restore(); //restore canvas state
}
canvas {border: 1px solid red}
<canvas id="myCanvas"></canvas>
<br/>
<input type="file" onchange="fileUpload(this.files)" id="file-input" capture="camera">
<br/>
<br/>
<button onclick="rotate()">Rotate</button>

关于HTML Canvas 在变换和旋转后重置绘图点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44822938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com