From 909c67cfba78b30295a78cfea4845086a4ac7acd Mon Sep 17 00:00:00 2001 From: Uwe Oestermeier Date: Thu, 28 Mar 2019 09:49:21 +0100 Subject: [PATCH] Added text that zooms vis font size changes, --- lib/pixi/index.html | 1 + lib/pixi/labeledgraphics.html | 45 ++++++++++++++++++++++++++++++++--- lib/pixi/labeledgraphics.js | 43 ++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/lib/pixi/index.html b/lib/pixi/index.html index f683403..8f15187 100644 --- a/lib/pixi/index.html +++ b/lib/pixi/index.html @@ -35,6 +35,7 @@ const index = new Index(itemTemplate, [ ['DeepZoom', 'deepzoom.html'], ['DeepZoomImage', 'deepzoomimage.html'], ['Flippable', 'flippable.html'], + ['LabeledGraphics', 'labeledgraphics.html'], ['List', 'list.html'], ['Message', 'message.html'], ['Modal', 'modal.html'], diff --git a/lib/pixi/labeledgraphics.html b/lib/pixi/labeledgraphics.html index 90e5806..69e9f37 100644 --- a/lib/pixi/labeledgraphics.html +++ b/lib/pixi/labeledgraphics.html @@ -23,23 +23,62 @@

JavaScript API

Let's look at an example:


- +

What you should see: A box with a label.

+

Zoomable Labels

+

Labeled graphics can also be used to represent zoomable text columns in more complex layouts. + Use the mousewheel to zoom the following text column: +

+ + + + diff --git a/lib/pixi/labeledgraphics.js b/lib/pixi/labeledgraphics.js index de7466a..775680a 100644 --- a/lib/pixi/labeledgraphics.js +++ b/lib/pixi/labeledgraphics.js @@ -130,6 +130,47 @@ export class Hypenate { } } +class TextLabel extends PIXI.Text { + + /** + *Creates an instance of TextLabel. + * @param {string} text - The string that you would like the text to display + * @param {object PIXI.TextStyle optional} style - The style parameters + * @param {canvas} + * @memberof TextLabel + */ + constructor(text, style=null, canvas=null, { minZoom = 0.1, maxZoom = 10} = {}) { + super(text, style, canvas ) + this.normFontSize = this.style.fontSize + this.minZoom = minZoom + this.maxZoom = maxZoom + } + + zoom(factor) { + let oldValue = parseFloat(this.style.fontSize) / this.normFontSize + let value = oldValue * factor + this.setZoom(value) + } + + setZoom(value) { + let oldValue = parseFloat(this.style.fontSize) / this.normFontSize + if (value > this.maxZoom) { + value = this.maxZoom + } + if (value < this.minZoom) { + value = this.minZoom + } + if (value != oldValue) { + this.style.fontSize = Math.max(value * this.normFontSize, 1) + } + } + + setZoomAndScale(scale) { + this.scale.set(1 / scale) + this.setZoom(scale) + } +} + /** * A specialization of the PIXI.Graphics class that allows to * resuse and place labels across different layout variants @@ -151,7 +192,7 @@ export class LabeledGraphics extends PIXI.Graphics { } _createText(label, fontInfo) { - return new PIXI.Text(label, fontInfo) + return new TextLabel(label, fontInfo) }