87,907
社区成员
发帖
与我相关
我的任务
分享
function rotateImg(direction) {
var min_step = 0;
var max_step = 3;
var step = $(currentSource).attr("step");
var id = $(".active")[0].id;
if (step == null) {
step = min_step;
}
if (direction == 'right') {
step++;
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
$(currentSource).attr("step", step);
var canvas = $("#canvas"+id)[0];
var width = canvas.width;
var height = canvas.height;
var src = canvas.toDataURL("image/png");
var img = new Image();
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = height;
canvas.height = width;
ctx.drawImage(currentSource, 0, 0,height,width);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(currentSource, 0, -height,width,height);
break;
case 2:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(currentSource, -height,-width ,height,width);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(currentSource, -width, 0,width,height);
break;
}
}
function rotateImg(direction) {
var min_step = 0;
var max_step = 3;
var step = $(currentSource).attr("step");
var id = $(".active")[0].id;
if (step == null) {
step = min_step;
}
if (direction == 'right') {
step++;
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
$(currentSource).attr("step", step);
var canvas = $("#canvas"+id)[0];
var width = canvas.width;
var height = canvas.height;
var src = canvas.toDataURL("image/png");
var imgs;
var img = new Image();
img.src = src;
img.setAttribute("step",step);
imgs = img;
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = height;
canvas.height = width;
ctx.drawImage(imgs, 0, 0,height,width);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, 0, -height,width,height);
break;
case 2:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, -height,-width ,height,width);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, -width, 0,width,height);
break;
}
}
function rotateImg(direction) {
var min_step = 0;
var max_step = 3;
var step = $(currentSource).attr("step");
var id = $(".active")[0].id;
if (step == null) {
step = min_step;
}
if (direction == 'right') {
step++;
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
console.log(step);
$(currentSource).attr("step", step);
var canvas = $("#canvas"+id)[0];
var width = canvas.width;
var height = canvas.height;
var src = canvas.toDataURL("image/png");
var imgs;
var img = new Image();
img.src = src;
img.setAttribute("step",step);
imgs = img;
imgs.onload = function () { //图片要等其加载完成之后才能成在canvas中使用
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = height;
canvas.height = width;
ctx.drawImage(imgs, 0, 0,height,width);
break;
case 1:
ctx.save();
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, 0, -height,width,height);
ctx.restore();
break;
case 2:
ctx.save();
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, -height,-width ,height,width);
ctx.restore();
break;
case 3:
ctx.save();
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, -width, 0,width,height);
ctx.restore();
break;
}
}
}
var imgs;
var img = new Image();
img.src = src;
img.setAttribute("step",step);
imgs = img;
imgs.onload = function () { //图片要等其加载完成之后才能成在canvas中使用
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = height;
canvas.height = width;
ctx.drawImage(imgs, 0, 0,height,width);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, 0, -height,width,height);
break;
case 2:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, -height,-width ,height,width);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(imgs, -width, 0,width,height);
break;
}
}