js实现框选截屏功能

实现的思路大概就是,先将dom转化为canvas画布,再对canvas进行裁切,然后通过canvas api生成图片,这里用到了一个库html2canvas

效果如图:
在这里插入图片描述
首先实现框选效果:

const mousedownEvent = (e) => {
	moveX = 0;
	moveY = 0;
	const [startX, startY] = [e.clientX, e.clientY];
	x = startX - viewer.getBoundingClientRect().left;
	y = startY - viewer.getBoundingClientRect().top;
	const divDom = document.createElement("div");
	divDom.id = 'screenshot';
	divDom.width = '1px';
	divDom.height = '1px';
	divDom.style.position = "absolute";
	divDom.style.top = y + "px";
	divDom.style.left = x + "px";
	const closeIcon = document.createElement("span");
	closeIcon.className = 'outline-close-icon';
	closeIcon.textContent = 'x';
	divDom.appendChild(closeIcon);

	closeIcon.addEventListener('click', () => {
		divDom.remove();
	});
	// document.body.appendChild(divDom)
	viewer.appendChild(divDom);
	const moveEvent = (e) => {
		moveX = e.clientX - startX;
		moveY = e.clientY - startY;
		if (moveX > 0) {
			divDom.style.width = moveX + 'px';
		} else {
			divDom.style.width = -moveX + 'px';
			divDom.style.left = e.clientX - viewer.getBoundingClientRect().left + 'px';
		}
		if (moveY > 0) {
			divDom.style.height = moveY + 'px';
		} else {
			divDom.style.height = -moveY + 'px';
			divDom.style.top = e.clientY - viewer.getBoundingClientRect().top + 'px';
		}
	};
	window.addEventListener("mousemove", moveEvent);
	window.addEventListener("mouseup", () => {
		window.removeEventListener("mousemove", moveEvent);
		window.removeEventListener("mousedown", mousedownEvent);
		document.querySelector("body").style.cursor = "default";
	});
};
window.addEventListener("mousedown", mousedownEvent);

全码:

const viewer = document.getElementById('viewer');

document.getElementById('screen-button').addEventListener('click', (e) => {

	document.querySelector("body").style.cursor = "crosshair";
	let moveX;
	let moveY;
	let x;
	let y;
	const mousedownEvent = (e) => {
		moveX = 0;
		moveY = 0;
		const [startX, startY] = [e.clientX, e.clientY];
		x = startX - viewer.getBoundingClientRect().left;
		y = startY - viewer.getBoundingClientRect().top;
		const divDom = document.createElement("div");
		divDom.id = 'screenshot';
		divDom.width = '1px';
		divDom.height = '1px';
		divDom.style.position = "absolute";
		divDom.style.top = y + "px";
		divDom.style.left = x + "px";
		const closeIcon = document.createElement("span");
		closeIcon.className = 'outline-close-icon';
		closeIcon.textContent = 'x';
		divDom.appendChild(closeIcon);

		closeIcon.addEventListener('click', () => {
			divDom.remove();
		});
		// document.body.appendChild(divDom)
		viewer.appendChild(divDom);
		const moveEvent = (e) => {
			moveX = e.clientX - startX;
			moveY = e.clientY - startY;
			if (moveX > 0) {
				divDom.style.width = moveX + 'px';
			} else {
				divDom.style.width = -moveX + 'px';
				divDom.style.left = e.clientX - viewer.getBoundingClientRect().left + 'px';
			}
			if (moveY > 0) {
				divDom.style.height = moveY + 'px';
			} else {
				divDom.style.height = -moveY + 'px';
				divDom.style.top = e.clientY - viewer.getBoundingClientRect().top + 'px';
			}
		};
		window.addEventListener("mousemove", moveEvent);
		window.addEventListener("mouseup", () => {

			window.removeEventListener("mousemove", moveEvent);
			window.removeEventListener("mousedown", mousedownEvent);
			document.querySelector("body").style.cursor = "default";

			if (!moveX) {
				return;
			}

			// 把body转成canvas
			html2canvas(viewer, {
				scale: 1,
				// allowTaint: true,
				useCORS: true  //跨域使用
			}).then(canvas2 => {
				let capture_x, capture_y;
				let width = moveX;
				let height = moveY;
				if (width > 0) {
					//从左往右画
					capture_x = startX - canvas.getBoundingClientRect().left + 1;
				} else {
					//从右往左画
					capture_x = x + width + 1;
				}
				if (height > 0) {
					//从上往下画
					capture_y = y + 1;
				} else {
					//从下往上画
					capture_y = y + height + 1;
				}
				printClip(canvas2, capture_x, capture_y, Math.abs(width), Math.abs(height));

				moveX = 0;
		
			});
		});
	};
	window.addEventListener("mousedown", mousedownEvent);
});

/**
 * 打印截取区域
 * @param canvas 截取的canvas
 * @param capture_x 截取的起点x
 * @param capture_y 截取的起点y
 * @param capture_width 截取的起点宽
 * @param capture_height 截取的起点高
 */
async function printClip(canvas2, capture_x, capture_y, capture_width, capture_height) {
	// 创建一个用于截取的canvas
	const clipCanvas = document.createElement('canvas');
	clipCanvas.width = capture_width;
	clipCanvas.height = capture_height;
	// 截取
	clipCanvas.getContext('2d').drawImage(canvas2, capture_x, capture_y, capture_width, capture_height, 0, 0, capture_width, capture_height);
	const clipImgBase64 = clipCanvas.toDataURL();
	// console.log('clipImgBase64->', clipImgBase64);
	// console.log('clipImgBase64->', clipImgBase64.replace(/^data:image\/\w+;base64,/, ""));
	const obj = {
		// file: new File([this.blob],'main.audio',{ type: 'audio/mp3' })
		file: new File([base64ToBlob(clipImgBase64.replace(/^data:image\/\w+;base64,/, ""), 'image/png')], 'test.png', { type: 'image/png' })
	};
	// 生成图片
	// var clipImg = new Image()
	// clipImg.src = clipImgBase64
	downloadIamge(clipImgBase64)
}

/**
 * 下载保存图片
 * @param imgUrl 图片地址
 */
function downloadIamge(imgUrl) {
	// 生成一个a元素
	const a = document.createElement('a');
	// 创建一个单击事件
	const event = new MouseEvent('click');
	// 生成文件名称
	const timestamp = new Date().getTime();
	const name = imgUrl.substring(22, 30) + timestamp + '.png';
	a.download = name;
	// 将生成的URL设置为a.href属性
	a.href = imgUrl;
	// 触发a的单击事件 开始下载
	a.dispatchEvent(event);
}

// 将Base64编码转换为Blob对象
function base64ToBlob(base64, type) {
	const byteCharacters = atob(base64);
	const byteArrays = [];

	for (let offset = 0; offset < byteCharacters.length; offset += 512) {
		let slice = byteCharacters.slice(offset, offset + 512);

		let byteNumbers = new Array(slice.length);
		for (let i = 0; i < slice.length; i++) {
			byteNumbers[i] = slice.charCodeAt(i);
		}

		let byteArray = new Uint8Array(byteNumbers);
		byteArrays.push(byteArray);
	}

	let blob = new Blob(byteArrays, { type: type });
	return blob;
}

坑:

  1. 如果生成图片样式有问题 html就用内联样式
  2. 当截图片的时候如果不识别 就将图片url转化为base64
  3. 当遇到超长截图的时候会失败,下面我详细讲下

我们截图的原理是将页面都绘制到了canvas上,但是这里canvas在不同浏览器中高度都是有限制的,而且不同浏览器 限制不一样,这样导致长篇幅的pdf页面绘制不了canvas,解决方式给一个最大高度限制,并且从可视区域开始截图,这样不管pdf预览页面再长 都可以去做框选截图了。

const viewer = document.getElementById('viewer');
		document.getElementById('screen-button').addEventListener('click', (e) => {

			document.querySelector("body").style.cursor = "crosshair";
			let moveX;
			let moveY;
			let x;
			let y;
			const mousedownEvent = (e) => {
				moveX = 0;
				moveY = 0;
				const [startX, startY] = [e.clientX, e.clientY];
				x = startX - viewer.getBoundingClientRect().left;
				y = startY - 64;
				const divDom = document.createElement("div");
				divDom.id = 'screenshot';
				divDom.width = '1px';
				divDom.height = '1px';
				divDom.style.position = "absolute";
				divDom.style.top = y + "px";
				divDom.style.left = x + "px";
				const closeIcon = document.createElement("span");
				closeIcon.className = 'outline-close-icon';
				closeIcon.textContent = 'x';
				divDom.appendChild(closeIcon);

				closeIcon.addEventListener('click', () => {
					divDom.remove();
				});
				// document.body.appendChild(divDom)
				viewer.appendChild(divDom);
				const moveEvent = (e) => {
					moveX = e.clientX - startX;
					moveY = e.clientY - startY;
					if (moveX > 0) {
						divDom.style.width = moveX + 'px';
					} else {
						divDom.style.width = -moveX + 'px';
						divDom.style.left = e.clientX - viewer.getBoundingClientRect().left + 'px';
					}
					if (moveY > 0) {
						divDom.style.height = moveY + 'px';
					} else {
						divDom.style.height = -moveY + 'px';
						divDom.style.top = e.clientY - viewer.getBoundingClientRect().top + 'px';
					}
				};
				window.addEventListener("mousemove", moveEvent);
				window.addEventListener("mouseup", () => {

					window.removeEventListener("mousemove", moveEvent);
					window.removeEventListener("mousedown", mousedownEvent);
					document.querySelector("body").style.cursor = "default";

					if (!moveX) {
						return;
					}
					// 把body转成canvas
					html2canvas(viewer, {
						scale: 1,
						height: (viewer.offsetHeight > 60000 ? 60000 : viewer.offsetHeight),
						width: viewer.scrollWidth,
						x: 0,
						y: document.getElementById('viewerContainer').scrollTop, // 用网页滚动的高度定位y轴顶点
						// dpi: 300,
						// allowTaint: true,
						useCORS: true,  //跨域使用
					}).then(canvas2 => {
						// document.body.append(canvas2);

						let capture_x, capture_y;
						let width = moveX;
						let height = moveY;
						if (width > 0) {
							//从左往右画
							capture_x = x + 1;
						} else {
							//从右往左画
							capture_x = x + width + 1;
						}
						if (height > 0) {
							//从上往下画
							capture_y = y + 1;
						} else {
							//从下往上画
							capture_y = y + height + 1;
						}
						printClip(canvas2, capture_x, capture_y, Math.abs(width), Math.abs(height));

						moveX = 0;
					});
				});
			};
			window.addEventListener("mousedown", mousedownEvent);
		});
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 C# 实现框选截图功能的代码示例: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class ScreenCapture : Form { private Point start; private Rectangle rect; public ScreenCapture() { this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Cursor = Cursors.Cross; this.MouseDown += ScreenCapture_MouseDown; this.MouseMove += ScreenCapture_MouseMove; this.MouseUp += ScreenCapture_MouseUp; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (Pen pen = new Pen(Color.Red, 2)) { e.Graphics.DrawRectangle(pen, rect); } } private void ScreenCapture_MouseDown(object sender, MouseEventArgs e) { start = new Point(e.X, e.Y); rect = new Rectangle(start, Size.Empty); } private void ScreenCapture_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point current = new Point(e.X, e.Y); rect.Location = new Point( Math.Min(start.X, current.X), Math.Min(start.Y, current.Y)); rect.Size = new Size( Math.Abs(start.X - current.X), Math.Abs(start.Y - current.Y)); Invalidate(); } } private void ScreenCapture_MouseUp(object sender, MouseEventArgs e) { if (rect.Width > 0 && rect.Height > 0) { using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(rect.Location, Point.Empty, rect.Size); } Clipboard.SetImage(bitmap); MessageBox.Show("截图已复制到剪贴板。"); } } Close(); } public static void Main() { Application.Run(new ScreenCapture()); } } ``` 这个程序创建了一个全屏的窗体,并在窗体上绘制了一个红色的矩形,用于框选截图。当用户按下鼠标左键并拖动时,矩形会跟随鼠标移动。当用户释放鼠标左键时,程序会将框选区域的屏幕截图复制到剪贴板,并关闭窗体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值