|
@@ -50,6 +50,7 @@ export class TextSprite extends THREE.Object3D{
|
|
|
this.text = text + '';
|
|
|
|
|
|
this.updateTexture();
|
|
|
+ this.sprite.waitUpdate() //重新计算各个viewport的matrix
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -86,60 +87,68 @@ export class TextSprite extends THREE.Object3D{
|
|
|
updateTexture(){
|
|
|
let canvas = document.createElement('canvas');
|
|
|
let context = canvas.getContext('2d');
|
|
|
- context.font = this.fontWeight + ' ' + this.fontsize + 'px ' + this.fontface;
|
|
|
+ const r = window.devicePixelRatio
|
|
|
+ context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface;
|
|
|
|
|
|
//context["font-weight"] = 100; //语法与 CSS font 属性相同。
|
|
|
- // get size data (height depends only on font size)
|
|
|
-
|
|
|
- //this.text = 'f 啊啊啊 jg'
|
|
|
+
|
|
|
+
|
|
|
+ //this.text = '啊啊啊啊啊啊fag'
|
|
|
|
|
|
let metrics = context.measureText(this.text );
|
|
|
let textWidth = metrics.width;
|
|
|
- let margin = this.margin || new THREE.Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) );
|
|
|
- let spriteWidth = 2 * margin.x + textWidth + 2 * this.rectBorderThick;
|
|
|
- let spriteHeight = 2 * margin.y + this.fontsize + 2 * this.rectBorderThick;
|
|
|
+ let margin = (this.margin ? new THREE.Vector2().copy(this.margin) : new THREE.Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) )).clone().multiplyScalar(r);
|
|
|
+ let spriteWidth = 2 * margin.x + textWidth + 2 * this.rectBorderThick * r ;
|
|
|
+ let spriteHeight = 2 * margin.y + this.fontsize * r + 2 * this.rectBorderThick * r;
|
|
|
context.canvas.width = spriteWidth;
|
|
|
context.canvas.height = spriteHeight;
|
|
|
- context.font = this.fontWeight + ' ' + this.fontsize + 'px ' + this.fontface;
|
|
|
-
|
|
|
+ context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface;
|
|
|
|
|
|
- let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值
|
|
|
+ /* let diff = 2//针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2
|
|
|
+
|
|
|
+ context.textBaseline = "middle"
|
|
|
+ */
|
|
|
+ let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) * r // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值
|
|
|
|
|
|
//canvas原点在左上角
|
|
|
context.textBaseline = 'alphabetic' // "middle" //设置文字基线。当起点y设置为0时,只有该线以下的部分被绘制出来。middle时文字显示一半(但是对该字体所有字的一半,有的字是不一定显示一半的,尤其汉字),alphabetic时是英文字母的那条基线。
|
|
|
|
|
|
- let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; // 当前文本字符串在这个字体下用的实际高度
|
|
|
+ //let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; // 当前文本字符串在这个字体下用的实际高度
|
|
|
|
|
|
//文字y向距离从textBaseline向上算
|
|
|
- let y = metrics.actualBoundingBoxAscent + margin.y + expand
|
|
|
+ let actualBoundingBoxAscent = metrics.actualBoundingBoxAscent == void 0 ? this.fontsize * r * 0.8 : metrics.actualBoundingBoxAscent //有的流览器没有。只能大概给一个
|
|
|
+ let y = actualBoundingBoxAscent + margin.y + expand
|
|
|
//console.log(this.text, 'y' , y, 'actualBoundingBoxAscent', metrics.actualBoundingBoxAscent,'expand',expand )
|
|
|
-
|
|
|
+
|
|
|
|
|
|
// border color
|
|
|
context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' +
|
|
|
this.borderColor.b + ',' + this.borderColor.a + ')';
|
|
|
-
|
|
|
- context.lineWidth = this.rectBorderThick;
|
|
|
+
|
|
|
+ let rectBorderThick = this.rectBorderThick * r;
|
|
|
+
|
|
|
+ context.lineWidth = rectBorderThick
|
|
|
// background color
|
|
|
context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' +
|
|
|
this.backgroundColor.b + ',' + this.backgroundColor.a + ')';
|
|
|
- this.roundRect(context, this.rectBorderThick / 2, this.rectBorderThick / 2,
|
|
|
- spriteWidth - this.rectBorderThick, spriteHeight - this.rectBorderThick, this.borderRadius);
|
|
|
+ this.roundRect(context, rectBorderThick / 2 , rectBorderThick / 2,
|
|
|
+ spriteWidth - rectBorderThick, spriteHeight - rectBorderThick, this.borderRadius * r);
|
|
|
|
|
|
// text color
|
|
|
if(this.textBorderThick){
|
|
|
context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' +
|
|
|
this.textBorderColor.b + ',' + this.textBorderColor.a + ')';
|
|
|
- context.lineWidth = this.textBorderThick;
|
|
|
- context.strokeText(this.text , this.rectBorderThick + margin.x, y /* spriteHeight/2 + expand */ );
|
|
|
+ context.lineWidth = this.textBorderThick * r;
|
|
|
+ context.strokeText(this.text , rectBorderThick + margin.x, y /* spriteHeight/2 + diff */ );
|
|
|
}
|
|
|
|
|
|
context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' +
|
|
|
this.textColor.b + ',' + this.textColor.a + ')';
|
|
|
- context.fillText(this.text , this.rectBorderThick + margin.x, y /* spriteHeight/2 + expand */);//x,y
|
|
|
+ context.fillText(this.text , rectBorderThick + margin.x, y/* spriteHeight/2 + diff */ );//x,y
|
|
|
+
|
|
|
|
|
|
let texture = new THREE.Texture(canvas);
|
|
|
- texture.minFilter = THREE.LinearFilter; //LinearMipMapLinearFilter会缩放到power of 2
|
|
|
+ texture.minFilter = THREE.LinearFilter;
|
|
|
texture.magFilter = THREE.LinearFilter;
|
|
|
texture.needsUpdate = true;
|
|
|
|
|
@@ -147,10 +156,9 @@ export class TextSprite extends THREE.Object3D{
|
|
|
this.sprite.material.map.dispose()
|
|
|
}
|
|
|
this.sprite.material.map = texture;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
- this.sprite.scale.set(spriteWidth * 0.01, spriteHeight * 0.01, 1.0);
|
|
|
+ this.sprite.scale.set(spriteWidth * 0.01 / r, spriteHeight * 0.01 / r, 1.0);
|
|
|
}
|
|
|
|
|
|
roundRect(ctx, x, y, w, h, r){
|