From a524aaf92ded75f6932233a9ee2dcf0135fc84de Mon Sep 17 00:00:00 2001 From: tengge1 <930372551@qq.com> Date: Sun, 7 Apr 2019 14:44:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B1=9E=E6=80=A7=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E4=BF=AE=E6=94=B9=E5=90=8D=E7=A7=B0=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=96=87=E5=AD=97=E5=87=A0=E4=BD=95=E4=BD=93=E6=96=87=E5=AD=97?= =?UTF-8?q?=E4=B8=8D=E6=94=B9=E5=8F=98bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../src/component/BasicComponent.js | 6 ++++ ShadowEditor.Web/src/object/geometry/Text.js | 31 +++++++++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ccafece0..a0f7994f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Language: 中文 / [繁體中文](README-tw.md) / [English](README-en.md) / [日 ## v0.1.9即将更新 +1. 修复属性面板修改名称时,文字几何体文字不改变bug。 ## v0.1.8更新 diff --git a/ShadowEditor.Web/src/component/BasicComponent.js b/ShadowEditor.Web/src/component/BasicComponent.js index ff43f1a9..fb3f21ef 100644 --- a/ShadowEditor.Web/src/component/BasicComponent.js +++ b/ShadowEditor.Web/src/component/BasicComponent.js @@ -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 () { diff --git a/ShadowEditor.Web/src/object/geometry/Text.js b/ShadowEditor.Web/src/object/geometry/Text.js index 9b2f68b6..c0763fd1 100644 --- a/ShadowEditor.Web/src/object/geometry/Text.js +++ b/ShadowEditor.Web/src/object/geometry/Text.js @@ -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; \ No newline at end of file