JavaScript canvas實(shí)現(xiàn)九宮格切圖效果
更新時(shí)間:2021年09月01日 11:48:34 作者:前端起步
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)九宮格切圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了canvas實(shí)現(xiàn)九宮格切圖效果的具體代碼,供大家參考,具體內(nèi)容如下
首先頁(yè)面展示

直接上代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
text-align: center;
}
canvas {
border: 1px solid;
}
.newcanvas {
width: 316px;
height: 316px;
margin: auto;
}
.newpohoto,
.download {
width: 300px;
height: 40px;
line-height: 40px;
margin: auto;
background-color: cornflowerblue;
border-radius: 5px;
cursor: pointer;
margin: 10px auto;
color: white;
}
</style>
</head>
<body>
<h3>使用canvas實(shí)現(xiàn)九宮格切圖的效果</h3>
<div class="mycanvas">
<canvas width="300" height="300" id="mycnavas"></canvas>
</div>
<div class="newpohoto">
開(kāi)始切圖
</div>
<div class="newcanvas">
<canvas width="100" height="100" id="img1"></canvas>
<canvas width="100" height="100" id="img2"></canvas>
<canvas width="100" height="100" id="img3"></canvas>
<canvas width="100" height="100" id="img4"></canvas>
<canvas width="100" height="100" id="img5"></canvas>
<canvas width="100" height="100" id="img6"></canvas>
<canvas width="100" height="100" id="img7"></canvas>
<canvas width="100" height="100" id="img8"></canvas>
<canvas width="100" height="100" id="img9"></canvas>
</div>
<div class="download">
下載圖片
</div>
<script>
var canvas = document.getElementById("mycnavas"); //現(xiàn)將圖片放上去
var cxt = mycnavas.getContext("2d");
var img = new Image();
img.src = "../img/img10.jpg";
window.onload = function() {
cxt.drawImage(img, 0, 0, 400, 300); //畫(huà)好圖片的位置
}
var arr = []; //將切的圖片存到數(shù)組里面
document.getElementsByClassName("newpohoto")[0].onclick = function() {
var q = 1;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var data = cxt.getImageData(j * 100, i * 100, 400, 100); //類似于“復(fù)制”功能
var img = document.getElementById("img" + q)
var newcxt = img.getContext("2d");
newcxt.putImageData(data, 0, 0); //類似“粘貼”功能
arr.push(console.log(img.toDataURL(q + ".png"))) //toDataURL()方法的兩個(gè)參數(shù):DataURL(type, encoderOptions) type指定轉(zhuǎn)換為base64編碼后圖片的格式,如:image/png、image/jpeg、image/webp等等,默認(rèn)為image/png格式;
q++;
console.log(arr)
}
}
}
//下載切的圖片
var arr = [];
document.getElementsByClassName('download')[0].onclick = function() {
for (var i = 0; i < 9; i++) {
var a = document.createElement('a');
a.download = 'img' + (i + 1);
a.href = arr[i];
document.body.appendChild(a);
a.click();
}
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript下拉框選項(xiàng)單擊事件的例子分享
這篇文章主要分享了一些javascript下拉框選項(xiàng)單擊事件的例子,以及在例子中遇到的問(wèn)題的解決方法,十分實(shí)用,推薦給小伙伴們參考下。2015-03-03
微信小程序使用for循環(huán)動(dòng)態(tài)渲染頁(yè)面操作示例
這篇文章主要介紹了微信小程序使用for循環(huán)動(dòng)態(tài)渲染頁(yè)面操作,結(jié)合實(shí)例形式分析了微信小程序使用for語(yǔ)句獲取data數(shù)據(jù)渲染頁(yè)面相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
bootstrap基礎(chǔ)知識(shí)學(xué)習(xí)筆記
這篇文章主要針對(duì)bootstrap基礎(chǔ)知識(shí)為大家整理了詳細(xì)的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
JS中利用FileReader實(shí)現(xiàn)上傳圖片前本地預(yù)覽功能
FileReader 對(duì)象允許Web應(yīng)用程序異步讀取存儲(chǔ)在用戶計(jì)算機(jī)上的文件(或原始數(shù)據(jù)緩沖區(qū))的內(nèi)容,使用 File 或 Blob 對(duì)象指定要讀取的文件或數(shù)據(jù)。下面通過(guò)本文給大家介紹JS中利用FileReader實(shí)現(xiàn)上傳圖片前本地預(yù)覽功能,需要的朋友參考下2018-03-03

