Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

three.texttexture

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

three.texttexture - npm Package Compare versions

Comparing version 17.9.10 to 17.10.21

23

package.json
{
"name": "three.texttexture",
"version": "17.9.10",
"version": "17.10.21",
"description": "A texture for writing text on its canvas.",
"main": "THREE.TextTexture.js",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"prepublish": "npm run build"
},
"repository": {

@@ -11,3 +16,3 @@ "type": "git",

"keywords": [
"threejs",
"three",
"canvas",

@@ -20,3 +25,2 @@ "text",

"material",
"aspect-ratio",
"3d",

@@ -34,3 +38,16 @@ "class",

"three": "^0.87.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-minify": "^0.2.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-runtime": "^6.26.0",
"eslint": "^4.9.0",
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-babel-minify": "^3.1.2",
"rollup-plugin-commonjs": "^8.2.1",
"rollup-plugin-node-resolve": "^3.0.0"
}
}

@@ -127,3 +127,11 @@ # THREE.TextTexture

`.text` • `.fontStyle` • `.fontVariant` • `.fontWeight` • `.fontSize` • `.fontFamily` • `.textAlign` • `.lineHeight` • `.padding`
`.text`<br/>
`.fontStyle`<br/>
`.fontVariant`<br/>
`.fontWeight`<br/>
`.fontSize`<br/>
`.fontFamily`<br/>
`.textAlign`<br/>
`.lineHeight`<br/>
`.padding`<br/>

@@ -144,3 +152,3 @@ Changing the value will redraw the canvas, if `autoRedraw` is `true`.

Make use of it, if you want change multiple properties at once. This way you will avoid unnecessary `.redraw()` calls.
Make use of it, if you want to change multiple properties at once. This way you will avoid unnecessary `.redraw()` calls.

@@ -147,0 +155,0 @@ ```javascript

279

THREE.TextTexture.js

@@ -1,278 +0,1 @@

(function(THREE) {
let measureText = (function() {
let ctx = document.createElement('canvas').getContext('2d');
return function(font, text) {
ctx.font = font;
return ctx.measureText(text);
};
})();
THREE.TextTexture = class extends THREE.Texture {
constructor({
autoRedraw = true,
text = '',
fontStyle = 'normal',
fontVariant = 'normal',
fontWeight = 'normal',
fontSize = 16,
fontFamily = 'sans-serif',
textAlign = 'center',
lineHeight = 1,
padding = 1/4,
magFilter = THREE.LinearFilter,
minFilter = THREE.LinearFilter,
mapping, wrapS, wrapT, format, type, anisotropy,
} = {}) {
super(document.createElement('canvas'), mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy);
this.autoRedraw = autoRedraw;
this._text = text;
this._fontStyle = fontStyle;
this._fontVariant = fontVariant;
this._fontWeight = fontWeight;
this._fontSize = fontSize;
this._fontFamily = fontFamily;
this._textAlign = textAlign;
this._lineHeight = lineHeight;
this._padding = padding;
/*
this._lines = undefined;
this._font = undefined;
this._textWidth = undefined;
this._textHeight = undefined;
*/
this.redraw();
}
redraw() {
let ctx = this.image.getContext('2d');
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
if (this.textWidth && this.textHeight) {
ctx.canvas.width = this.textWidth + this.paddingInPixels * 2;
ctx.canvas.height = this.textHeight + this.paddingInPixels * 2;
ctx.font = this.font;
ctx.textAlign = this.textAlign;
ctx.textBaseline = 'middle';
ctx.fillStyle = 'white';
let left = this.paddingInPixels + (() => {
switch (ctx.textAlign.toLowerCase()) {
case 'left':
return 0;
case 'right':
return this.textWidth;
case 'center':
return this.textWidth / 2;
}
})();
let top = this.paddingInPixels + this.fontSize / 2;
for (let line of this.lines) {
ctx.fillText(line, left, top);
top += this.lineHeightInPixels;
}
} else {
ctx.canvas.width = ctx.canvas.height = 1;
}
this.needsUpdate = true;
}
_redrawIfAuto() {
if (this.autoRedraw) {
this.redraw();
}
}
get text() {
return this._text;
}
set text(value) {
if (this._text !== value) {
this._text = value;
this._lines = undefined;
this._textWidth = undefined;
this._textHeight = undefined;
this._redrawIfAuto();
}
}
_computeLines() {
if (this.text) {
return this.text.split('\n');
}
return [];
}
get lines() {
if (this._lines === undefined) {
this._lines = this._computeLines();
}
return this._lines;
}
get linesCount() {
return this.lines.length;
}
get fontStyle() {
return this._fontStyle;
}
set fontStyle(value) {
if (this._fontStyle !== value) {
this._fontStyle = value;
this._font = undefined;
this._textWidth = undefined;
this._redrawIfAuto();
}
}
get fontVariant() {
return this._fontVariant;
}
set fontVariant(value) {
if (this._fontVariant !== value) {
this._fontVariant = value;
this._font = undefined;
this._textWidth = undefined;
this._redrawIfAuto();
}
}
get fontWeight() {
return this._fontWeight;
}
set fontWeight(value) {
if (this._fontWeight !== value) {
this._fontWeight = value;
this._font = undefined;
this._textWidth = undefined;
this._redrawIfAuto();
}
}
get fontSize() {
return this._fontSize;
}
set fontSize(value) {
if (this._fontSize !== value) {
this._fontSize = value;
this._font = undefined;
this._textWidth = undefined;
this._textHeight = undefined;
this._redrawIfAuto();
}
}
get fontFamily() {
return this._fontFamily;
}
set fontFamily(value) {
if (this._fontFamily !== value) {
this._fontFamily = value;
this._font = undefined;
this._textWidth = undefined;
this._redrawIfAuto();
}
}
_computeFont() {
return [
this.fontStyle,
this.fontVariant,
this.fontWeight,
`${this.fontSize}px`,
this.fontFamily,
].join(' ');
}
get font() {
if (this._font === undefined) {
this._font = this._computeFont();
}
return this._font;
}
get textAlign() {
return this._textAlign;
}
set textAlign(value) {
if (this._textAlign !== value) {
this._textAlign = value;
this._redrawIfAuto();
}
}
get lineHeight() {
return this._lineHeight;
}
set lineHeight(value) {
if (this._lineHeight !== value) {
this._lineHeight = value;
this._textHeight = undefined;
this._redrawIfAuto();
}
}
get lineHeightInPixels() {
return this.fontSize * this.lineHeight;
}
_computeTextWidth() {
let returns = 0;
for (let line of this.lines) {
returns = Math.max(measureText(this.font, line).width, returns);
}
return returns;
}
get textWidth() {
if (this._textWidth === undefined) {
this._textWidth = this._computeTextWidth();
}
return this._textWidth;
}
_computeTextHeight() {
return this.fontSize * (this.lineHeight * (this.linesCount - 1) + 1);
}
get textHeight() {
if (this._textHeight === undefined) {
this._textHeight = this._computeTextHeight();
}
return this._textHeight;
}
get padding() {
return this._padding;
}
set padding(value) {
if (this._padding !== value) {
this._padding = value;
this._redrawIfAuto();
}
}
get paddingInPixels() {
return this.fontSize * this.padding;
}
get aspect() {
if (this.image.width && this.image.height) {
return this.image.width / this.image.height;
}
return 1;
}
};
})(THREE);
(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?module.exports=b(require('three')):'function'==typeof define&&define.amd?define(['three'],b):(a.THREE=a.THREE||{},a.THREE.TextTexture=b(a.THREE))})(this,function(a){'use strict';function b(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}function c(a,b){if(!a)throw new ReferenceError('this hasn\'t been initialised - super() hasn\'t been called');return b&&('object'==typeof b||'function'==typeof b)?b:a}function d(a,b){if('function'!=typeof b&&null!==b)throw new TypeError('Super expression must either be null or a function, not '+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}a=a&&a.hasOwnProperty('default')?a['default']:a;var e=document.createElement('canvas').getContext('2d'),f=function(a,b){return e.font=a,e.measureText(b)},g=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,'value'in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),h=function(e){function h(){var d=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=d.autoRedraw,f=d.text,g=void 0===f?'':f,i=d.fontStyle,j=void 0===i?'normal':i,k=d.fontVariant,l=void 0===k?'normal':k,m=d.fontWeight,n=void 0===m?'normal':m,o=d.fontSize,p=void 0===o?16:o,q=d.fontFamily,r=void 0===q?'sans-serif':q,s=d.textAlign,t=void 0===s?'center':s,u=d.lineHeight,v=void 0===u?1:u,w=d.padding,x=void 0===w?1/4:w,y=d.magFilter,z=void 0===y?a.LinearFilter:y,A=d.minFilter,B=void 0===A?a.LinearFilter:A,C=d.mapping,D=d.wrapS,E=d.wrapT,F=d.format,G=d.type,H=d.anisotropy;b(this,h);var I=c(this,(h.__proto__||Object.getPrototypeOf(h)).call(this,document.createElement('canvas'),C,D,E,z,B,F,G,H));return I.autoRedraw=void 0===e||e,I._text=g,I._fontStyle=j,I._fontVariant=l,I._fontWeight=n,I._fontSize=p,I._fontFamily=r,I._textAlign=t,I._lineHeight=v,I._padding=x,I.redraw(),I}return d(h,e),g(h,[{key:'redraw',value:function(){var a=this.image.getContext('2d');if(a.clearRect(0,0,a.canvas.width,a.canvas.height),this.textWidthInPixels&&this.textHeightInPixels){a.canvas.width=this.imageWidthInPixels,a.canvas.height=this.imageHeightInPixels,a.font=this.font,a.textBaseline='middle',a.fillStyle='white';var b;switch(this.textAlign){case'left':a.textAlign='left',b=this.paddingInPixels;break;case'right':a.textAlign='right',b=this.paddingInPixels+this.textWidthInPixels;break;case'center':a.textAlign='center',b=this.paddingInPixels+this.textWidthInPixels/2;}var c,d=this.paddingInPixels+this.fontSize/2,e=!0,f=!1;try{for(var g,h,i=this.lines[Symbol.iterator]();!(e=(g=i.next()).done);e=!0)h=g.value,a.fillText(h,b,d),d+=this.lineHeightInPixels}catch(a){f=!0,c=a}finally{try{!e&&i.return&&i.return()}finally{if(f)throw c}}}else a.canvas.width=a.canvas.height=1;this.needsUpdate=!0}},{key:'_redrawIfAuto',value:function(){this.autoRedraw&&this.redraw()}},{key:'_computeLines',value:function(){return this.text?this.text.split('\n'):[]}},{key:'_computeFont',value:function(){return[this.fontStyle,this.fontVariant,this.fontWeight,this.fontSize+'px',this.fontFamily].join(' ')}},{key:'_computeTextWidthInPixels',value:function(){var a,b=0,c=!0,d=!1;try{for(var e,g,h=this.lines[Symbol.iterator]();!(c=(e=h.next()).done);c=!0)g=e.value,b=Math.max(f(this.font,g).width,b)}catch(b){d=!0,a=b}finally{try{!c&&h.return&&h.return()}finally{if(d)throw a}}return b}},{key:'_computeTextHeight',value:function(){return this.lineHeight*(this.linesCount-1)+1}},{key:'text',get:function(){return this._text},set:function(a){this._text!==a&&(this._text=a,this._lines=void 0,this._textWidth=void 0,this._textHeight=void 0,this._redrawIfAuto())}},{key:'lines',get:function(){return void 0===this._lines&&(this._lines=this._computeLines()),this._lines}},{key:'linesCount',get:function(){return this.lines.length}},{key:'fontStyle',get:function(){return this._fontStyle},set:function(a){this._fontStyle!==a&&(this._fontStyle=a,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}},{key:'fontVariant',get:function(){return this._fontVariant},set:function(a){this._fontVariant!==a&&(this._fontVariant=a,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}},{key:'fontWeight',get:function(){return this._fontWeight},set:function(a){this._fontWeight!==a&&(this._fontWeight=a,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}},{key:'fontSize',get:function(){return this._fontSize},set:function(a){this._fontSize!==a&&(this._fontSize=a,this._font=void 0,this._textWidth=void 0,this._textHeight=void 0,this._redrawIfAuto())}},{key:'fontFamily',get:function(){return this._fontFamily},set:function(a){this._fontFamily!==a&&(this._fontFamily=a,this._font=void 0,this._textWidth=void 0,this._redrawIfAuto())}},{key:'font',get:function(){return void 0===this._font&&(this._font=this._computeFont()),this._font}},{key:'textAlign',get:function(){return this._textAlign},set:function(a){this._textAlign!==a&&(this._textAlign=a,this._redrawIfAuto())}},{key:'lineHeight',get:function(){return this._lineHeight},set:function(a){this._lineHeight!==a&&(this._lineHeight=a,this._textHeight=void 0,this._redrawIfAuto())}},{key:'lineHeightInPixels',get:function(){return this.fontSize*this.lineHeight}},{key:'textWidthInPixels',get:function(){return void 0===this._textWidth&&(this._textWidth=this._computeTextWidthInPixels()),this._textWidth}},{key:'textHeight',get:function(){return void 0===this._textHeight&&(this._textHeight=this._computeTextHeight()),this._textHeight}},{key:'textHeightInPixels',get:function(){return this.fontSize*this.textHeight}},{key:'padding',get:function(){return this._padding},set:function(a){this._padding!==a&&(this._padding=a,this._redrawIfAuto())}},{key:'paddingInPixels',get:function(){return this.fontSize*this.padding}},{key:'imageWidthInPixels',get:function(){return this.textWidthInPixels+2*this.paddingInPixels}},{key:'imageHeight',get:function(){return this.textHeight+2*this.padding}},{key:'imageHeightInPixels',get:function(){return this.textHeightInPixels+2*this.paddingInPixels}},{key:'aspect',get:function(){return this.image.width&&this.image.height?this.image.width/this.image.height:1}}]),h}(a.Texture);return Object.assign(a,{TextTexture:h}),h});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc