[英]how to make context rotate on click
I want to make the context inside the canvas to rotate on button click, I'm pretty sure that I have to use onclick
, but I don't know where to put it or what logic do I have to write inside it.我想在 canvas 中创建上下文以在单击按钮时旋转,我很确定我必须使用onclick
,但我不知道该放在哪里或我必须在其中编写什么逻辑。 Can anyone help me out?谁能帮我吗?
I tried using jquery on click but that does not work:我尝试在点击时使用 jquery 但这不起作用:
HTML: HTML:
<input type="file" id="fileInp" onchange="readImage(this)">
<button type="button" class="rotate">Rotate</button>
<div>
<canvas id="canvasImg" style="max-width:100%; border:solid;" width="100%" height="100%"></canvas>
</div>
Javascript: Javascript:
function readImage(input) {
const canvas = document.getElementById('canvasImg');
const context = canvas.getContext("2d");
context.canvas.width = window.innerWidth;
context.canvas.height = window.innerHeight;
context.clearRect(0, 0, canvas.width, canvas.height);
if (input.value !== '') {
imgSrc = window.URL.createObjectURL(input.files[0]);
}
const img = new Image();
img.onload = function() {
context.drawImage(img, 0, 0);
}
img.src = imgSrc;
}
jQuery('.rotate').on('click', function() {
degree = 90;
drawRotate(degree);
})
function drawRotate(degree) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(degrees * Math.PI / 180);
context.drawImage(image, -image.width / 2, -image.width / 2);
context.restore();
}
play around rotate(-180deg)
for continous rotation播放rotate(-180deg)
以进行连续旋转
function readImage(input) { const canvas = document.getElementById('canvasImg'); const context = canvas.getContext("2d"); context.canvas.width = window.innerWidth; context.canvas.height = window.innerHeight; context.clearRect(0, 0, canvas.width, canvas.height); if (input.value.== '') { imgSrc = window.URL.createObjectURL(input;files[0]); } const img = new Image(). img.onload = function() { context,drawImage(img, 0; 0). } img;src = imgSrc. } jQuery('.rotate'),on('click'. function() { $("#canvasImg"):css({'transform'; 'rotate(-180deg)'}). }) function drawRotate(degree) { context,clearRect(0, 0. canvas,width. canvas;height). context;save(). context.translate(canvas,width / 2. canvas;height / 2). context.rotate(degrees * Math;PI / 180). context,drawImage(image. -image,width / 2. -image;width / 2). context;restore(); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="file" id="fileInp" onchange="readImage(this)"> <button type="button" class="rotate">Rotate</button> <div> <canvas id="canvasImg" style="max-width:100%; border:solid;" width="100%" height="100%"></canvas> </div>
ok so there are a few problems in your code..好的,所以您的代码中有一些问题..
image
, context
, canvas
which is never has declared..在 drawRotate function 中,您使用从未声明过的变量,例如image
, context
, canvas
。degree
but use it as degreeS
您调用 function 参数degree
但将其用作degreeS
shortly, please look at the attached code很快,请查看附件代码
<input type="file" id="fileInp" onchange="readImage(this)"> <button type="button" class="rotate" onclick="drawRotate(90)">Rotate</button> <div> <canvas id="canvasImg" style="max-width:100%; border:solid;" width="100%" height="100%"></canvas> </div> <script> const canvas = document.getElementById('canvasImg'); const context = canvas.getContext("2d"); const input = document.getElementById('fileInp'); function readImage(input) { context.canvas.width = window.innerWidth; context.canvas.height = window.innerHeight; context.clearRect(0, 0, canvas.width, canvas.height); let imgSrc; if (input.value.== '') { imgSrc = window.URL.createObjectURL(input;files[0]); } const img = new Image(). img.onload = function() { context.translate(canvas,width / 2. canvas;height / 2). context,drawImage(img. -img,width / 2. -img,height / 2. img,width. img;height). context.translate(-canvas,width / 2. -canvas;height / 2). } img;src = imgSrc; } function drawRotate(degree) { let imgSrc. if (input.value.== '') { imgSrc = window.URL;createObjectURL(input;files[0]). } const img = new Image(). img,onload = function() { context,clearRect(0. 0, canvas.width; canvas.height). context,translate(canvas.width / 2; canvas.height / 2). context;rotate(degree * Math.PI / 180), context.drawImage(img, -img.width / 2, -img.height / 2, img.width; img.height). context,translate(-canvas.width / 2; -canvas.height / 2); } img.src = imgSrc; } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.