修复属性面板修改名称时,文字几何体文字不改变bug。

This commit is contained in:
tengge1 2019-04-07 14:44:17 +08:00
parent 2cf549246c
commit a524aaf92d
3 changed files with 35 additions and 3 deletions

View File

@ -8,6 +8,7 @@ Language: 中文 / [繁體中文](README-tw.md) / [English](README-en.md) / [日
## v0.1.9即将更新
1. 修复属性面板修改名称时文字几何体文字不改变bug。
## v0.1.8更新

View File

@ -2,6 +2,7 @@ import BaseComponent from './BaseComponent';
import SetValueCommand from '../command/SetValueCommand';
import RemoveObjectCommand from '../command/RemoveObjectCommand';
import AddObjectCommand from '../command/AddObjectCommand';
import Text from '../object/geometry/Text';
/**
* 基本信息组件
@ -117,6 +118,11 @@ BasicComponent.prototype.onChangeName = function () {
var editor = this.app.editor;
editor.execute(new SetValueCommand(this.selected, 'name', name.getValue()));
// bug: https://gitee.com/tengge1/ShadowEditor/issues/IV1V3
if (this.selected instanceof Text) {
this.selected.updateText(name.getValue());
}
};
BasicComponent.prototype.onChangeVisible = function () {

View File

@ -10,13 +10,13 @@ function Text(text = L_TEXT) {
var fontSize = 64;
var ctx = canvas.getContext('2d');
ctx.font = `${fontSize}px sans-serif`;
ctx.font = `${fontSize}px 'Microsoft YaHei'`;
var textMetrics = ctx.measureText(text);
canvas.width = StringUtils.makePowOfTwo(textMetrics.width);
canvas.width = textMetrics.width;
canvas.height = fontSize;
ctx.textBaseline = 'hanging';
ctx.font = `${fontSize}px sans-serif`; // 重新设置画布大小前面设置的ctx属性全部失效
ctx.font = `${fontSize}px 'Microsoft YaHei'`; // 重新设置画布大小前面设置的ctx属性全部失效
ctx.fillStyle = 'rgba(0,0,0,0)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
@ -24,6 +24,7 @@ function Text(text = L_TEXT) {
ctx.fillText(text, (canvas.width - textMetrics.width) / 2, 0);
var map = new THREE.CanvasTexture(canvas);
map.minFilter = THREE.LinearFilter;
var geometry = new THREE.PlaneBufferGeometry(canvas.width / 10, canvas.height / 10);
var material = new THREE.MeshBasicMaterial({
@ -40,4 +41,28 @@ function Text(text = L_TEXT) {
Text.prototype = Object.create(THREE.Mesh.prototype);
Text.prototype.constructor = Text;
Text.prototype.updateText = function (text) {
this.name = text;
var canvas = this.material.map.image;
var fontSize = 64;
var ctx = canvas.getContext('2d');
ctx.font = `${fontSize}px 'Microsoft YaHei'`;
var textMetrics = ctx.measureText(text);
canvas.width = textMetrics.width;
canvas.height = fontSize;
ctx.textBaseline = 'hanging';
ctx.font = `${fontSize}px 'Microsoft YaHei'`; // 重新设置画布大小前面设置的ctx属性全部失效
ctx.fillStyle = 'rgba(0,0,0,0)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'rgba(255,255,255,1)';
ctx.fillText(text, (canvas.width - textMetrics.width) / 2, 0);
this.material.map.needsUpdate = true;
};
export default Text;