@vuemap/vue-amap-extra
Version:
@vuemap/vue-amap扩展库,包含threejs相关图层
169 lines (164 loc) • 4.88 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var tween_js = require('@tweenjs/tween.js');
var threeLayer = require('@vuemap/three-layer');
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
class CustomThreeGltf extends threeLayer.ThreeGltf {
constructor(layer, options, $vue, callback) {
options.onLoaded = (gltf) => {
gltf.userData.acceptEvent = true;
gltf.userData.$vue = $vue;
if (callback) {
callback();
}
};
super(layer, options);
__publicField(this, "moveAnimation");
__publicField(this, "prePosition");
// 前一次位置信息
__publicField(this, "prePositionTween");
__publicField(this, "moveFrame", -1);
// 缓慢移动的动画帧
__publicField(this, "preAngle");
// 前一次角度信息
__publicField(this, "preAngleTween");
__publicField(this, "angleFrame", -1);
this.moveAnimation = options.moveAnimation;
}
setScale(scale) {
let scaleArray;
if (typeof scale === "number") {
scaleArray = [scale, scale, scale];
} else {
scaleArray = scale;
}
this.object.scale.set(...scaleArray);
}
setPosition(position) {
const positionConvert = this.layer.convertLngLat(position);
if (!this.moveAnimation || !this.moveAnimation.smooth) {
this._updatePosition(positionConvert);
this.prePosition = positionConvert;
} else {
if (!this.prePosition) {
this._updatePosition(positionConvert);
this.prePosition = positionConvert;
} else {
this._updatePositionTween(positionConvert);
}
}
}
_updatePositionTween(newPosition) {
var _a;
if (!this.prePosition) {
return;
}
if (this.prePositionTween) {
this.prePositionTween.end();
}
const preObj = {
x: this.prePosition[0],
y: this.prePosition[1]
};
this._stopMoveAnimation();
const duration = ((_a = this.moveAnimation) == null ? void 0 : _a.duration) || 1e3;
this.prePositionTween = new tween_js.Tween(preObj).to({
x: newPosition[0],
y: newPosition[1]
}).duration(duration).easing(tween_js.Easing.Linear.None).onUpdate(() => {
this._updatePosition([preObj.x, preObj.y]);
}).start();
this._moveAnimate();
this.prePosition = newPosition;
}
_moveAnimate() {
var _a;
(_a = this.prePositionTween) == null ? void 0 : _a.update();
this.moveFrame = requestAnimationFrame(() => {
this._moveAnimate();
});
}
_stopMoveAnimation() {
cancelAnimationFrame(this.moveFrame);
}
_updatePosition(position) {
this.object.position.setX(position[0]);
this.object.position.setY(position[1]);
this.refresh();
}
setAngle(angle) {
if (!this.moveAnimation || !this.moveAnimation.smooth) {
this._updateAngle(angle);
this.preAngle = angle;
} else {
if (this.preAngle === void 0) {
this._updateAngle(angle);
this.preAngle = angle;
} else {
this._updateAngleTween(angle);
}
}
}
_updateAngle(angle) {
const x = this.object.rotation.x;
const z = this.object.rotation.z;
const y = Math.PI / 180 * angle;
this.object.rotation.set(x, y, z);
this.refresh();
}
_updateAngleTween(angle) {
var _a;
if (this.preAngle === void 0) {
return;
}
if (this.preAngleTween) {
this.preAngleTween.end();
}
const preObj = {
angle: this.preAngle
};
this._stopAngleAnimation();
const duration = ((_a = this.moveAnimation) == null ? void 0 : _a.duration) || 1e3;
this.preAngleTween = new tween_js.Tween(preObj).to({
angle
}).duration(duration).easing(tween_js.Easing.Linear.None).onUpdate(() => {
this._updateAngle(preObj.angle);
}).onComplete(() => {
this._stopAngleAnimation();
}).start();
this._angleAnimate();
this.preAngle = angle;
}
_angleAnimate() {
var _a;
(_a = this.preAngleTween) == null ? void 0 : _a.update();
this.angleFrame = requestAnimationFrame(() => {
this._angleAnimate();
});
}
_stopAngleAnimation() {
cancelAnimationFrame(this.angleFrame);
}
setMoveAnimation(move) {
this.moveAnimation = move;
}
destroy() {
this._stopAngleAnimation();
this._stopMoveAnimation();
this.prePosition = void 0;
this.preAngle = void 0;
this.prePositionTween = void 0;
this.preAngleTween = void 0;
if (this.object) {
this.object.$vue = null;
}
super.destroy();
}
}
exports.default = CustomThreeGltf;
//# sourceMappingURL=CustomThreeGltf.js.map