[英]Resize a canvas image on click and center that with javascript
如何縮放<canvas>
標簽中包含的照片? 特別是,我想在用戶單擊時放大照片。
縮放並不難:
img.width = img.width + 100;
img.height = img.height + 100;
ctx.drawImage(img,0,0,img.width,img.height);
問題是,我也想像普通放大鏡一樣將縮放后的圖像定位在點擊位置上。
數據
R
Cw
, Ch
Iw
, Ih
Ix
, Iy
Pcx
, Pcy
Pox
, Poy
Prx
, Pry
方法
Pox = Pcx - Ix
, Poy = Pcy - Iy
Prx = Pox * R
, Pry = Poy * R
top = (Ch / 2) - Pry
, left = (Cw / 2) - Prx
ctx.drawImage(img, left, top, img.width, img.height)
履行
// resize image
I.w *= R;
I.h *= R;
// canvas pos -> image pos
Po.x = Pc.x - I.left;
Po.y = Pc.y - I.top;
// old img pos -> resized img pos
Pr.x = Po.x * R;
Pr.y = Po.y * R;
// center the point
I.left = (C.w / 2) - Pr.x;
I.top = (C.h / 2) - Pr.y;
// draw image
ctx.drawImage(img, I.left, I.top, I.w, I.h);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.