預覽連接 點擊預覽 vue
methods:funName() - 對應源碼中methods中的funName方法git
data:dataName - 對應源碼中data中的dataName數據github
input[type="file"]
彈出選擇圖片框,js 主動觸發點擊事件;objectURL = URL.createObjectURL(blob)
;須要掌握的 canvas 相關知識:canvas
ctx.clearRect(x,y,width,height)
;ctx.fillRect(x,y,width,height)
;ctx.arc(x,y,r,startAngle,endAngle,counterclockwise)
; 繪製矩形 ctx.rect(x,y,width,height);
# 語法
ctx.drawImage(image, dx, dy);
ctx.drawImage(image, dx, dy, dWidth, dHeight);
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
# 參數
image # 繪製的元素(能夠爲HTMLImageElement,HTMLVideoElement,或者 HTMLCanvasElement。)
dx,dy # 目標畫布(destination canvas)左上角的座標
dWidth,dHeight # 目標畫布(destination canvas)上繪製圖像寬高
sx,sy # 源畫布(source canvase)左上角的座標
sWidth,sHeight # 源畫布(source canvase)選擇的圖像寬高
複製代碼
ctx.clip()
;具體步驟:bash
裁剪區域vue data示意圖: 服務器
![]()
知識點: onmousedown、onmousemove、onmouseupapp
具體實現:ide
methods:drag()ui
記錄鼠標座標,鼠標移動根據偏移量計算圓心位置。this
canvas.onmousedown = e => {
let [lastX, lastY] = [e.offsetX, e.offsetY];
self.movement = true;
canvas.onmousemove = e => {
self.circleCenter = {
X:
self.cropperCanvasSize.width > 2 * self.slectRadius
? self.circleCenter.X + (e.offsetX - lastX)
: self.cropperCanvasSize.width / 2,
Y:
self.cropperCanvasSize.height > 2 * self.slectRadius
? self.circleCenter.Y + (e.offsetY - lastY)
: self.cropperCanvasSize.height / 2
};
self.renderCropperImg();
[lastX, lastY] = [e.offsetX, e.offsetY];
};
canvas.onmouseup = e => {
self.movement = false;
canvas.onmousemove = null;
canvas.onmouseup = null;
};
};
複製代碼
知識點:
具體實現:
methods:upload()
this.$refs.preview.toBlob((blob)=> {
const url = URL.createObjectURL(blob);
const formData = new FormData();
formData.append(this.uploadProps.name, blob, `${Date.now()}.png`);
if(this.data){
Object.keys(this.uploadProps.data).forEach(key => {
formData.append(key, this.uploadProps.data[key]);
});
}
const request = new XMLHttpRequest();
request.open("POST", this.uploadProps.action, true);
request.send(formData);
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
// ...
}
};
});
複製代碼