@visactor/vrender-core
Version:
## Description
114 lines (108 loc) • 4.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.createImage = exports.Image = void 0;
const graphic_1 = require("./graphic"), config_1 = require("./config"), theme_1 = require("./theme"), application_1 = require("../application"), constants_1 = require("./constants"), common_outer_boder_bounds_1 = require("./graphic-service/common-outer-boder-bounds"), IMAGE_UPDATE_TAG_KEY = [ "width", "height", "image", ...graphic_1.GRAPHIC_UPDATE_TAG_KEY ];
class Image extends graphic_1.Graphic {
constructor(params) {
super(params), this.type = "image", this.numberType = constants_1.IMAGE_NUMBER_TYPE,
this.loadImage(this.attribute.image);
}
getImageElement() {
const {image: image} = this.attribute;
if (!image || !this.resources) return null;
const res = this.resources.get(image);
return "success" !== res.state ? null : res.data;
}
get width() {
return this.tryUpdateAABBBounds(), this._actualWidth;
}
get height() {
return this.tryUpdateAABBBounds(), this._actualHeight;
}
get repeatX() {
var _a;
return null !== (_a = this.attribute.repeatX) && void 0 !== _a ? _a : "no-repeat";
}
set repeatX(repeatX) {
this.attribute.repeatX === repeatX && (this.attribute.repeatX = repeatX);
}
get repeatY() {
var _a;
return null !== (_a = this.attribute.repeatY) && void 0 !== _a ? _a : "no-repeat";
}
set repeatY(repeatY) {
this.attribute.repeatY === repeatY && (this.attribute.repeatY = repeatY);
}
get image() {
return this.attribute.image;
}
set image(image) {
image !== this.attribute.image && (this.attribute.image = image, this.loadImage(this.attribute.image));
}
imageLoadSuccess(url, image, cb) {
super.imageLoadSuccess(url, image, (() => {
this.successCallback && this.successCallback();
})), this.addUpdateBoundTag();
}
imageLoadFail(url, cb) {
super.imageLoadFail(url, (() => {
this.failCallback && this.failCallback();
}));
}
setAttributes(params, forceUpdateTag, context) {
return params.image && this.loadImage(params.image), super.setAttributes(params, forceUpdateTag, context);
}
setAttribute(key, value, forceUpdateTag, context) {
return "image" === key && this.loadImage(value), super.setAttribute(key, value, forceUpdateTag, context);
}
getGraphicTheme() {
return (0, theme_1.getTheme)(this).image;
}
updateAABBBounds(attribute, imageTheme, aabbBounds) {
if (!this.updatePathProxyAABBBounds(aabbBounds)) {
const {maxWidth: maxWidth = imageTheme.maxWidth, maxHeight: maxHeight = imageTheme.maxHeight} = attribute;
let {width: width, height: height} = attribute;
if (null == width || null == height) {
const imageElement = this.getImageElement();
if (imageElement) {
const imageWidth = imageElement.width, imageHeight = imageElement.height;
if (null != width) height = width * (imageHeight / imageWidth); else if (null != height) width = height * (imageWidth / imageHeight); else {
const imageRatio = imageWidth / imageHeight;
imageRatio > maxWidth / maxHeight ? (width = maxWidth, height = maxWidth / imageRatio) : (height = maxHeight,
width = maxHeight * imageRatio);
}
} else width = maxWidth, height = maxHeight;
}
this._actualWidth = width, this._actualHeight = height, aabbBounds.set(0, 0, width, height);
}
const {tb1: tb1, tb2: tb2} = application_1.application.graphicService.updateTempAABBBounds(aabbBounds);
return (0, common_outer_boder_bounds_1.updateBoundsOfCommonOuterBorder)(attribute, imageTheme, tb1),
aabbBounds.union(tb1), tb1.setValue(tb2.x1, tb2.y1, tb2.x2, tb2.y2), application_1.application.graphicService.transformAABBBounds(attribute, aabbBounds, imageTheme, !1, this),
aabbBounds;
}
getDefaultAttribute(name) {
return config_1.DefaultImageAttribute[name];
}
needUpdateTags(keys) {
return super.needUpdateTags(keys, IMAGE_UPDATE_TAG_KEY);
}
needUpdateTag(key) {
return super.needUpdateTag(key, IMAGE_UPDATE_TAG_KEY);
}
clone() {
return new Image(Object.assign({}, this.attribute));
}
getNoWorkAnimateAttr() {
return Image.NOWORK_ANIMATE_ATTR;
}
}
function createImage(attributes) {
return new Image(attributes);
}
exports.Image = Image, Image.NOWORK_ANIMATE_ATTR = Object.assign({
image: 1,
repeatX: 1,
repeatY: 1
}, graphic_1.NOWORK_ANIMATE_ATTR), exports.createImage = createImage;
//# sourceMappingURL=image.js.map