konva.js 工具类

konva.js 工具类

js 复制代码
import StringUtils from "./StringUtil.js";

class KonvaCanvas {
	/**
	 * 初始化画布
	 * @param {String} domId 容器dom id
	 */
	constructor(domId) {
		this.layer = null;
		this.stage = null;
		this.scale = 1.0;

		// 加载时就放大缩小的倍数,只是记录一下,后续不再更改此值
		this.scaleX = 0;
		this.scaleY = 0;
		// 鼠标滚轮控制的放大缩小倍数
		this.scaleByX = 1;
		this.scaleByY = 1;

		this.init(domId);
	}

	/**
	 * 聚焦到指定元素
	 * @param {String} elementId 元素dom id
	 */
	focusOn(elementId) {
		if (!this.layer || !this.stage || !elementId) return;
		var element = this.stage.find("#" + elementId)[0];

		let element_x = StringUtils.isNumeric(element.position().x) ? element.position().x : Number(element.position().x);
		let element_y = StringUtils.isNumeric(element.position().y) ? element.position().y : Number(element.position().y);

		let x = this.stage.width() * 0.5 - element_x;
		let y = this.stage.height() * 0.5 - element_y;

		this.stage.scaleX(1.0);
		this.stage.scaleY(1.0);
		this.scale = 1.0;
		this.stage.x(x);
		this.stage.y(y);

		// this.layer.draw();
	}

	/**
	 * 绘制图片
	 * @param {String} url 图片路径
	 * @param {String} id 唯一标识
	 * @param {Number} x 横坐标
	 * @param {Number} y 纵坐标
	 * @param {Number} zoom 缩放比例
	 * @param {Number} rotate 旋转角度
	 */
	drawImage(url, id, x = 0, y = 0, zoom = 1.0, rotate = 0) {
		return new Promise((resolve, reject) => {
			var img = new Image();
			img.src = url;

			const that = this;
			img.onload = function () {
				const img_w = img.width * zoom;
				const img_h = img.height * zoom;

				var kImage = new Konva.Image({
					image: img,
					x: x,
					y: y,
					width: img_w,
					height: img_h,
					offset: {
						x: img_w / 2,
						y: img_h / 2,
					},
					id: id,
				});
				kImage.rotate(rotate);
				that.layer.add(kImage);
				console.log(`图片加载完成`);
				resolve(kImage);
			};
		});
	}

	/**
	 * 更新元素属性信息
	 * @param {String} id 元素id
	 * @param {Object} attr 元素属性 {}
	 */
	updateElement(id, attr) {
		if (!this.layer || !this.stage) return;

		var shape = this.stage.find("#" + id)[0];
		if (shape) {
			if (attr.x) {
				attr.x = isNaN(Number(attr.x)) ? 0 : Number(attr.x);
			}
			if (attr.y) {
				attr.y = isNaN(Number(attr.y)) ? 0 : Number(attr.y);
			}
			if (attr.rotation) {
				attr.rotation = isNaN(Number(attr.rotation)) ? 0 : Number(attr.rotation);
			}

			attr = {
				...shape.getAttrs(),
				...attr,
			};

			shape.setAttrs(attr);
			this.stage.batchDraw(); //重绘
		}
	}

	/**
	 * 绘制线
	 * @param {Array} points 点位坐标   [x1, y1, x2, y2, x3, y3]
	 * @param {String} id 唯一标识
	 * @param {Number} lineWidth 线宽
	 * @param {String} color 颜色
	 * @returns
	 */
	drawLine(points, id, lineWidth = 1, color = "#ffffff") {
		if (!this.layer) return;

		var line = new Konva.Line({
			points: points || [],
			stroke: color,
			strokeWidth: lineWidth,
			lineCap: "round",
			lineJoin: "round",
			tension: 0.5,
			id: id,
		});

		this.layer.add(line);
	}

	/**
	 * 销毁画布
	 */
	destroyed() {
		if (this.layer) {
			this.layer.destroyChildren();
			this.layer.draw();
		}
	}

	// ==========================================  私有方法
	init(domId) {
		if (!domId) {
			return;
		}

		const container = document.getElementById(domId);
		var width = container.clientWidth;
		var height = container.clientHeight;
		var stage = new Konva.Stage({
			container: domId,
			width: width,
			height: height,
			draggable: true,
		});
		var layer = new Konva.Layer();
		stage.add(layer);

		this.stage = stage;
		this.layer = layer;

		this.stage.on("wheel", e => {
			const direction = e.evt.deltaY > 0 ? -0.05 : 0.05;
			this.scale = this.scale + direction;
			this.stage.scaleX(this.scale);
			this.stage.scaleY(this.scale);
			this.stage.draw();
		});
	}
	// ==========================================  私有方法 end
}

export default KonvaCanvas;
相关推荐
晓得迷路了13 分钟前
栗子前端技术周刊第 156 期 - jQuery 4.1、TanStack Query v6 路线图...
前端·javascript·node.js
CoderYanger6 小时前
Java EE 进阶:13. Spring事务和事务传播机制(第二弹)
java·开发语言·数据库·spring boot·后端·spring·java-ee
星恒随风10 小时前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制
linux·服务器·开发语言·c++·人工智能
yingjie11011 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
Felven12 小时前
C. Make Them Equal
c语言·开发语言·前端
tryCbest12 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
风落青芜14 小时前
排序算法(四):归并排序、计数排序与算法总结
c语言·开发语言·数据结构·算法·排序算法
Resean022314 小时前
讲透Java(14)泛型的本质:类型擦除与桥接方法
java·开发语言
apcipot_rain19 小时前
C++开发 面试 1Day速通
开发语言·c++·面试
时间的情敌20 小时前
Three.js GLTF 阀门 / 泵体模型加载、材质修改、状态联动(正常 / 故障 / 告警变色)
开发语言·javascript·three.js·材质