首页 > 其他分享 >js canvas 照片旋转 demo

js canvas 照片旋转 demo

时间:2023-03-21 11:33:55浏览次数:45  
标签:canvas img demo 0.5 js width context height

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/><!--以手机屏幕为标准,禁止放大缩小-->
<meta name="format-detection" content="telephone=no"/><!--屏蔽IOS下自动转换数字为手机链接-->
<meta name="apple-mobile-web-app-capable" content="yes" /><!--IOS应用模式,自动隐藏默认的工具栏和菜单栏-->
<meta name="browsermode" content="application"><!--UC应用模式-->
<meta name="x5-page-mode" content="app"><!--腾讯X5浏览器应用模式-->
<title>照片旋转</title>
</head>
<body style="margin: 0;">
<input type="file" onchange="fileChangeEvent(this.files[0]);"/><br><br>
<img style="width: 100%;" src="./1.jpg"/><br><br>
<canvas style="display: none;"></canvas>
<input type="button" value="90度"/><br><br>
<input type="button" value="180度"/><br><br>
<input type="button" value="270度"/><br><br>
<input type="button" value="返回"/><br><br>
</body>
<script type="text/javascript">
var img = document.querySelector("img");
var canvas = document.querySelector("canvas");
var context = canvas.getContext('2d');
var imgHeight = 200;
var imgWidth = 200;
img.onload = function(){
imgHeight = img.height;
imgWidth = img.width;
}
function fileChangeEvent(file){
var URL = window.URL || window.webkitURL;
img.src = URL.createObjectURL(file);
img.onload = function(){
imgHeight = img.height;
imgWidth = img.width;
}
}

function rotate90()
{
img.style.width = "initial"; // 防止 css 导致旋转时的图片宽高比拉伸而导致的模糊问题
canvas.width = imgHeight;
canvas.height = imgWidth;
context.translate(canvas.width * 0.5, canvas.height * 0.5);
context.rotate(Math.PI * 0.5);
context.translate(-canvas.height * 0.5, -canvas.width * 0.5);
context.drawImage(img, 0, 0, imgWidth, imgHeight);
img.src = canvas.toDataURL("image/png");
}

function rotate180()
{
canvas.width = imgWidth;
canvas.height = imgHeight;
context.rotate(Math.PI);
context.translate(-canvas.width, -canvas.height);
context.drawImage(img, 0, 0, imgWidth, imgHeight);
img.src = canvas.toDataURL("image/png");
}

function rotate270()
{
img.style.width = "initial"; // 防止 css 导致旋转时的图片宽高比拉伸而导致的模糊问题
canvas.width = imgHeight;
canvas.height = imgWidth;
// 以中心点为基准旋转,所有的旋转只改变了 canvas 画笔的坐标轴,对画布没有任何影响
context.translate(canvas.width * 0.5, canvas.height * 0.5);
context.rotate(-Math.PI * 0.5);
context.translate(-canvas.height * 0.5, -canvas.width * 0.5);
context.drawImage(img, 0, 0, imgWidth, imgHeight);
img.src = canvas.toDataURL("image/png");
}
document.querySelectorAll("input[type='button']")[0].onclick = function(){
rotate90();
};

document.querySelectorAll("input[type='button']")[1].onclick = function(){
rotate180();
};

document.querySelectorAll("input[type='button']")[2].onclick = function(){
rotate270();
};

document.querySelectorAll("input[type='button']")[3].onclick = function(){
history.back();
};
</script>
</html>

标签:canvas,img,demo,0.5,js,width,context,height
From: https://blog.51cto.com/u_3871599/6139961

相关文章

  • jquery.fly.min.js 拋物插件
    插件官方:[url]https://github.com/amibug/fly[/url],官方例子:[url]http://codepen.io/hzxs1990225/full/ogLaVp[/url]首先加载jQuery.js和jquer......
  • JS 知识点收集
    js文件中import中加{}和不加{}的区别参考网址https://blog.csdn.net/baidu_38225647/article/details/104968662大括号的加与不加取决于import来源的js文件-如果......
  • js判断一段字符串中某字符出现的个数
    问题点在一个字符串中,如"abc,cde",我们要找出"c"出现的次数。本文章将详细说明方法思路。str为某字符串char为某字符//得到字符串含有某个字符的个数方法一function......
  • umijs服务器代理配置,多个代理
    同时代理api和资源://服务器代理proxy:{'/api':{target:'http://xxx',changeOrigin:true,pathRewrite:{'^/api/':'/api/',......
  • Can not set java.lang.String field com.jsedc.log.pojo.entity.voSyslogV0.happenT
    未加泛型约束的result,其List中的实体对象会被序列化为LinkedHashMap,实际结构为Result<List<LinkedHashMap<String,String>>>导出excel时对象赋值失败......
  • 什么是webpack、npm、node、nodejs?他们之间有什么区别?
     最近在学一学前端的东西,发现前端技术栈有几个概念有些分不清,比如接触Vue后,对Vue-Cli有了解后,仅仅知道Vue-Cli是一个Vue项目的脚手架,可以快速的构建一个Vue的基于Npm的模......
  • Unbuntu22.04使用NVM安装NodeJS
    一、使用NVM(NodeVersionManager)1.在NVMgithub上的readme获取一行命令curl-o-https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh|bash或wge......
  • js实现trim的功能
    trim:function(str){returnstr.replace(/(^\s*)|(\s*$)/g,"");}ltrim:function(str){returnstr.replace(/(^\s*)/g,"");}rtrim:function(str){returnstr.replace(......
  • js创建自定义对话框
    一:这是只是一个很简单的例子createDialog:function(flag){vardialogMark=document.getElementById("dialogMark");va......
  • JSP显示拦截器的ActionContext内容
    拦截器有:atx.put("login_message","请登陆,再使用系统.");jsp显示message用:Errors:${login_message}。......