如何缩放包含在<canvas>标签中的照片?特别是,我想放大用户点击点上的照片。
缩放并不难做到:
img.width = img.width + 100;
img.height = img.height + 100;
ctx.drawImage(img,0,0,img.width,img.height);问题是,我也想把放大的图像放在点击点的中心,就像普通的放大镜一样。
发布于 2010-10-11 02:21:58
Data
R
Ih
Ch,Iw,Ch图像位置:Iw,原始图像上的PcyIw>D24>,调整大小的图像上的Poy>D28>,Pry
方法
Pox = Pcx - Ix,Poy = Pcy - Iy Prx = Pox * R,Pry = Poy * Rtop = (Ch / 2) - Pry,left = (Cw / 2) - Prxctx.drawImage(img, left, top, img.width, img.height)Implementation
// 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);https://stackoverflow.com/questions/3901413
复制相似问题