js實(shí)現(xiàn)圖片旋轉(zhuǎn) js滾動(dòng)鼠標(biāo)中間對(duì)圖片放大縮小
從開(kāi)通博客園到今天,有兩個(gè)多月了。我發(fā)現(xiàn)之前沒(méi)有開(kāi)通博客記錄自己所做的東西,真是后悔啊。
現(xiàn)在一點(diǎn)一點(diǎn)把自己所做的功能以博客的形式記錄下來(lái),一方面可以給大家分享,大家一起學(xué)習(xí),同時(shí)自己也從新回顧一下。
這個(gè)圖片放大,縮小和旋轉(zhuǎn),我采用canvas畫(huà)布這個(gè)來(lái)做的,核心點(diǎn)就在js中去控制鼠標(biāo)狀態(tài)及事件。
我先給大家展示一下效果圖。
鼠標(biāo)移到畫(huà)布范圍內(nèi)就會(huì)出現(xiàn)下方的操作欄,每次以90度選擇。
1.在引入js的時(shí)候一定要注意了,由于在使用畫(huà)布canvas時(shí),需要等圖片加載完成后才可以執(zhí)行畫(huà)布里的內(nèi)容。js要在最后引入。
2.js中要在圖片加載完成之后在方法
主要的地方就是這個(gè)啦,其它就是js方法了,我就不一一解釋了,有js功底的能看懂,如果有地方不懂,或者需要改進(jìn)的就在下面評(píng)論出來(lái),大家一起學(xué)習(xí)。
下面我就貼出代碼了,需要演示項(xiàng)目源碼的小伙伴也評(píng)論出來(lái),我把演示項(xiàng)目發(fā)出來(lái)。
這是目錄結(jié)構(gòu),也不需要什么jar包。image下面就是圖片啦。
html頁(yè)面代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="../js/jquery.js"></script> <link rel="stylesheet" type="text/css" href="../css/pictureCss.css" rel="external nofollow" /> <link > </head> <body> <div id="pandiv"> <img src="../image/3.png" style="display: none;"> <canvas id="canvas" width="700" height="500" style="cursor: default;"> </canvas> <div id="control" style="display: none;"> <img id="left" src="../image/left1.png" onclick="rateImage(270)"> <img id="right" src="../image/right1.png" onclick="rateImage(90)"> </div> </div> <script type="text/javascript" src="../js/pictureJs.js"></script> </body> </html>
css樣式代碼
@CHARSET "UTF-8"; * { margin: 0px; padding: 0px; } #pandiv { width: 700px; height: 500px; } #control { background: #ccc; opacity: 0.7; width: 200px; height: 30px; display: none; padding-top: 5px; position: absolute; left: 250px; top: 450px; } #canvas { border: 1px solid black; } #left { float: left; display: block; } #right { float: right; display: block; }
核心重點(diǎn)js代碼:
/** * */ var canvas = document.getElementById("canvas"); var pandiv = document.getElementById("pandiv"); var cxt = canvas.getContext("2d"); var control = document.getElementById("control"); var imgScale = 1; var img; var imgX = 0; var imgY = 0; var currentRate = 0; /**當(dāng)前的旋轉(zhuǎn)角度*/ var mouseDownLocation; var isMouseDown = false; window.onload = function() { var bbox = canvas.getBoundingClientRect(); var imageUrl = $("#pandiv>img").attr("src"); img = new Image(); img.src = imageUrl; img.id = "pic"; loadImage(); drawImage(); } function reLoadImage() { loadImage(); } function loadImage() { if (img.width <= canvas.width && img.height <= canvas.height) { imgX = (canvas.width - img.width * imgScale) / 2 imgY = (canvas.height - img.height * imgScale) / 2; } else { var ratio = img.width / img.height; widthTime = img.width / canvas.width; heightTime = img.height / canvas.height; if (widthTime > heightTime) { img.width = canvas.width; img.height = canvas.width / ratio; } else { img.height = canvas.height; img.width = canvas.height * ratio; } imgX = (canvas.width - img.width * imgScale) / 2 imgY = (canvas.height - img.height * imgScale) / 2 } } //var backGroundColor = ['#223344', '#445566', '#667788', '#778899']; //var backGroundColorIndex = 0; function drawImage() { var bbox = canvas.getBoundingClientRect(); //cxt.clearRect(0, 0, canvas.width, canvas.height); cxt.clearRect(-200, -200, canvas.width * 2, canvas.height * 2); // cxt.fillStyle = backGroundColor[backGroundColorIndex++ % backGroundColor.length]; //cxt.fillRect(0, 0, canvas.width, canvas.height); cxt.drawImage(img, imgX, imgY, img.width * imgScale, img.height * imgScale); } // windowToCanvas此方法用于鼠標(biāo)所在點(diǎn)的坐標(biāo)切換到畫(huà)布上的坐標(biāo) function windowToCanvas(canvas, x, y) { var bbox = canvas.getBoundingClientRect(); return { x : x - bbox.left - (bbox.width - canvas.width) / 2, y : y - bbox.top - (bbox.height - canvas.height) / 2 }; } function isPointInImageArea(point) { return true; //return (point.x > imgX && point.x < imgX + img.width * imgScale && //point.y > imgY && point.y < imgY + img.height * imgScale); } function isPointInCanvasArea(point) { return true; //var bbox = canvas.getBoundingClientRect(); //return (point.x > bbox.left && point.x < bbox.right && point.y > bbox.//top && point.y < bbox.bottom); } function isDivArea(point) { return true; //var bbox =pandiv.getBoundingClientRect(); //return (point.x > bbox.left && point.x < bbox.right && point.y > bbox.//top && point.y < bbox.bottom); } canvas.onmousewheel = canvas.onwheel = function(event) { var pos = windowToCanvas(canvas, event.clientX, event.clientY); event.wheelDelta = event.wheelDelta ? event.wheelDelta : (event.deltaY * (-40)); if (event.wheelDelta > 0) { //alert("放大"); if (isPointInImageArea(pos)) { imgScale *= 2; //imgX = imgX * 2 - pos.x; // imgY = imgY * 2 - pos.y; imgX = (canvas.width - img.width * imgScale) / 2 imgY = (canvas.height - img.height * imgScale) / 2 } else { imgScale *= 2; //imgX = (canvas.width - img.width * imgScale) / 2; //imgY = (canvas.height - img.height * imgScale) / 2; imgX = (canvas.width - img.width * imgScale) / 2 imgY = (canvas.height - img.height * imgScale) / 2 } } else { //alert("縮小"); if (isPointInImageArea(pos)) { imgScale /= 2; //imgX = imgX * 0.5 + pos.x * 0.5; // imgY = imgY * 0.5 + pos.y * 0.5; imgX = (canvas.width - img.width * imgScale) / 2 imgY = (canvas.height - img.height * imgScale) / 2 } else { imgScale /= 2; // imgX = (canvas.width - img.width * imgScale) / 2; // imgY = (canvas.height - img.height * imgScale) / 2; imgX = (canvas.width - img.width * imgScale) / 2 imgY = (canvas.height - img.height * imgScale) / 2 } } drawImage(); return false; } /**旋轉(zhuǎn)angle度*/ function rateImage(angle) { currentRate = (currentRate + angle) % 360; cxt.clearRect(0, 0, canvas.width, canvas.height); //cxt.save(); cxt.translate(canvas.width / 2, canvas.height / 2); cxt.save(); cxt.rotate(angle * Math.PI / 180); cxt.translate(-canvas.width / 2, -canvas.height / 2); imgScale = 1; reLoadImage(); drawImage(); //cxt.restore(); } /**鼠標(biāo)按下*/ pandiv.onmousedown = function(event) { mouseDownLocation = windowToCanvas(canvas, event.clientX, event.clientY); if (isPointInImageArea(mouseDownLocation)) { isMouseDown = true; document.title = 'mouse down'; } } /**鼠標(biāo)彈起*/ document.body.onmouseup = function() { isMouseDown = false; canvas.style.cursor = "default"; document.title = 'mouse up'; } /**鼠標(biāo)移動(dòng)*/ pandiv.onmousemove = function(event) { if (isMouseDown) { canvas.style.cursor = "move"; var newMouseLocation = windowToCanvas(canvas, event.clientX, event.clientY); if (isDivArea({ x : event.clientX, y : event.clientY })) { var x = newMouseLocation.x - mouseDownLocation.x; var y = newMouseLocation.y - mouseDownLocation.y; mouseDownLocation = newMouseLocation; /**根據(jù)角度,計(jì)算圖片偏移*/ if (0 == currentRate) { imgX += x; imgY += y; } else if (90 == currentRate) { imgX += y; imgY -= x; } else if (180 == currentRate) { imgX -= x; imgY -= y; } else if (270 == currentRate) { imgX -= y; imgY += x; } } else { /** 鼠標(biāo)移動(dòng)至畫(huà)布范圍外,置鼠標(biāo)彈起 */ isMouseDown = false; canvas.style.cursor = "default"; document.title = 'mouse up'; } drawImage(); } } pandiv.onmouseover = function() { //alert("1"); control.style.display = "block"; } canvas.onmouseout = function() { //alert("1"); control.style.display = "none"; }
這就是實(shí)現(xiàn)這個(gè)圖片旋轉(zhuǎn),放大,縮小的演示代碼。
由于這幾天在做一個(gè)切換圖片的功能,點(diǎn)擊上一頁(yè),下一頁(yè)實(shí)現(xiàn)圖片切換,這個(gè)功能以及快全部實(shí)現(xiàn)了,到時(shí)候我搭建一個(gè)框架的演示項(xiàng)目,來(lái)給大家展示圖片切換上一張,下一張,也包括旋轉(zhuǎn),放大縮小功能。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript進(jìn)階練習(xí)及簡(jiǎn)單實(shí)例分析
下面小編就為大家?guī)?lái)一篇JavaScript進(jìn)階練習(xí)及簡(jiǎn)單實(shí)例分析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06JavaScript的事件綁定(方便不支持js的時(shí)候)
看了JavaScript DOM 編程藝術(shù)的Best Practices那章,才知道我們?cè)谥谱骶W(wǎng)頁(yè)的時(shí)候有很多東西需要考慮2013-10-10JavaScript實(shí)現(xiàn)無(wú)限輪播效果
這篇文章主要介為大家詳細(xì)紹了JavaScript實(shí)現(xiàn)無(wú)限輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11使用原生的javascript來(lái)實(shí)現(xiàn)輪播圖
這篇文章主要介紹了使用原生的javascript來(lái)實(shí)現(xiàn)輪播圖,在代碼底部給大家補(bǔ)充了原生javascript實(shí)現(xiàn)banner圖自動(dòng)輪播切換 ,需要的朋友可以參考下2017-02-02詳解JavaScript如何創(chuàng)建一個(gè)非自動(dòng)播放的GIF網(wǎng)絡(luò)組件
這篇文章主要為大家介紹了如何利用JavaScript創(chuàng)建一個(gè)允許您的用戶(hù)決定是否要播放gif的Web組件,文中的實(shí)現(xiàn)步驟講解詳細(xì),需要的可以參考一下2022-02-02用原生JS對(duì)AJAX做簡(jiǎn)單封裝的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇用原生JS對(duì)AJAX做簡(jiǎn)單封裝的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07Javascript 頁(yè)面模板化很多人沒(méi)有使用過(guò)的方法
今天遇到一個(gè)問(wèn)題,這個(gè)問(wèn)題也是我以前遇到的問(wèn)題,以前的方式,也是大多數(shù)人使用的方式。大家可以看看我的文章2012-06-06js實(shí)現(xiàn)無(wú)縫滾動(dòng)特效
這篇文章主要介紹了js實(shí)現(xiàn)無(wú)縫滾動(dòng)特效,結(jié)合已學(xué)知識(shí)進(jìn)行擴(kuò)展性練習(xí),感興趣的朋友可以參考一下2015-12-12