图像引用实体 (ImageRefEnt)
大约 2 分钟
图像引用实体 (ImageRefEnt)
ImageRefEnt 表示外部图像的引用,将光栅图像插入到 CAD 图纸中。
概述
图像引用允许在矢量图纸中插入位图图像(如 PNG、JPG 等),常用于添加参考图、照片、标志等。
构造函数
import { ImageRefEnt, Point2D } from 'vjcad';
// 简化写法(推荐)
const image1 = new ImageRefEnt([100, 100], 0, 1); // 插入点, 旋转角度, 缩放因子
image1.url = '/images/logo.png';
image1.setDefaults();
// 分步设置属性
const image2 = new ImageRefEnt();
image2.url = '/images/logo.png';
image2.insertionPoint = [100, 100]; // 支持数组写法
image2.setDefaults();简化写法
构造函数和 insertionPoint 属性都支持 [x, y] 数组形式的坐标。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
imagePath | string | 图像文件路径 |
insertPoint | Point2D | 插入点(左下角) |
width | number | 显示宽度 |
height | number | 显示高度 |
rotation | number | 旋转角度(弧度) |
brightness | number | 亮度(0-100) |
contrast | number | 对比度(0-100) |
fade | number | 淡化度(0-100) |
示例
插入图像
import { Engine, ImageRefEnt, Point2D } from 'vjcad';
const image = new ImageRefEnt();
image.imagePath = '/assets/reference.png';
image.insertPoint = new Point2D(0, 0);
image.width = 200;
image.height = 150;
image.setDefaults();
Engine.addEntities(image);插入带旋转的图像
import { Engine, ImageRefEnt, Point2D } from 'vjcad';
const image = new ImageRefEnt();
image.imagePath = '/assets/photo.jpg';
image.insertPoint = new Point2D(100, 100);
image.width = 100;
image.height = 100;
image.rotation = Math.PI / 6; // 30°
image.setDefaults();
Engine.addEntities(image);调整图像显示效果
import { Engine, ImageRefEnt, Point2D } from 'vjcad';
const image = new ImageRefEnt();
image.imagePath = '/assets/background.png';
image.insertPoint = new Point2D(0, 0);
image.width = 500;
image.height = 400;
image.brightness = 50; // 默认亮度
image.contrast = 50; // 默认对比度
image.fade = 70; // 淡化 70%
image.setDefaults();
Engine.addEntities(image);注意事项
- 图像路径:可以是相对路径或绝对路径,确保在运行时可以访问
- 图像格式:支持常见的 Web 图像格式(PNG、JPG、GIF、WebP)
- 性能考虑:大图像可能影响渲染性能,建议优化图像大小
- 打印输出:图像在打印时可能与屏幕显示有差异