123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- var THREEx = THREEx || {}
- THREEx.DynamicTexture = function(width, height){
- var canvas = document.createElement( 'canvas' )
- canvas.width = width
- canvas.height = height
- this.canvas = canvas
- var context = canvas.getContext( '2d' )
- this.context = context
- var texture = new THREE.Texture(canvas)
- this.texture = texture
- }
- THREEx.DynamicTexture.prototype.clear = function(fillStyle){
-
- if( fillStyle !== undefined ){
- this.context.fillStyle = fillStyle
- this.context.fillRect(0,0,this.canvas.width, this.canvas.height)
- }else{
- this.context.clearRect(0,0,this.canvas.width, this.canvas.height)
- }
-
- this.texture.needsUpdate = true;
-
- return this;
- }
- THREEx.DynamicTexture.prototype.drawText = function(text, x, y, fillStyle, contextFont){
-
- if( contextFont !== undefined ) this.context.font = contextFont;
-
- if( x === undefined || x === null ){
- var textSize = this.context.measureText(text);
- x = (this.canvas.width - textSize.width) / 2;
- }
-
- this.context.fillStyle = fillStyle;
- this.context.fillText(text, x, y);
-
- this.texture.needsUpdate = true;
-
- return this;
- };
- THREEx.DynamicTexture.prototype.drawTextCooked = function(options){
- var context = this.context
- var canvas = this.canvas
- options = options || {}
- var text = options.text
- var params = {
- margin : options.margin !== undefined ? options.margin : 0.1,
- lineHeight : options.lineHeight !== undefined ? options.lineHeight : 0.1,
- align : options.align !== undefined ? options.align : 'left',
- fillStyle : options.fillStyle !== undefined ? options.fillStyle : 'black',
- font : options.font !== undefined ? options.font : "bold "+(0.2*512)+"px Arial",
- }
-
- console.assert(typeof(text) === 'string')
- context.save()
- context.fillStyle = params.fillStyle;
- context.font = params.font;
- var y = (params.lineHeight + params.margin)*canvas.height
- while(text.length > 0 ){
-
- var maxText = computeMaxTextLength(text)
-
- text = text.substr(maxText.length)
-
- var textSize = context.measureText(maxText);
- if( params.align === 'left' ){
- var x = params.margin*canvas.width
- }else if( params.align === 'right' ){
- var x = (1-params.margin)*canvas.width - textSize.width
- }else if( params.align === 'center' ){
- var x = (canvas.width - textSize.width) / 2;
- }else console.assert( false )
-
- this.context.fillText(maxText, x, y);
-
- y += params.lineHeight*canvas.height
- }
- context.restore()
-
- this.texture.needsUpdate = true;
-
- return this;
- function computeMaxTextLength(text){
- var maxText = ''
- var maxWidth = (1-params.margin*2)*canvas.width
- while( maxText.length !== text.length ){
- var textSize = context.measureText(maxText);
- if( textSize.width > maxWidth ) break;
- maxText += text.substr(maxText.length, 1)
- }
- return maxText
- }
- }
- THREEx.DynamicTexture.prototype.drawImage = function(/* same params as context2d.drawImage */){
-
- this.context.drawImage.apply(this.context, arguments)
-
- this.texture.needsUpdate = true;
-
- return this;
- }
|