This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
/
textcanvas_layer.ts
103 lines (91 loc) · 3.4 KB
/
textcanvas_layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
// tslint:disable-next-line:no-var-requires
const Stats = require("stats.js");
import * as THREE from "three";
import { FontCatalog, TextCanvas, TextRenderStyle } from "@here/harp-text-canvas";
/**
* This example showcases how you can add text to different layers of [[TextCanvas]], to preserve
* proper rendering order independently from addition order.
*
* For more information regarding basic [[TextCanvas]] initialization and usage, please check:
* [[TextCanvasMinimalExample]] documentation.
*/
export namespace TextCanvasLayerExample {
const stats = new Stats();
let webglRenderer: THREE.WebGLRenderer;
let camera: THREE.OrthographicCamera;
let textCanvas: TextCanvas;
let assetsLoaded: boolean = false;
function addText() {
const position = new THREE.Vector3(30.0, 0.0, 0.0);
textCanvas.textRenderStyle.fontSize!.size *= 4.0;
for (let i = 9; i >= 0; --i) {
textCanvas.textLayoutStyle.lineRotation = i * ((2.0 * Math.PI) / 10.0);
textCanvas.textRenderStyle.color = new THREE.Color(
Math.random(),
Math.random(),
Math.random()
);
textCanvas.addText(String(i), position, { layer: i });
}
}
function onWindowResize() {
webglRenderer.setSize(window.innerWidth, window.innerHeight);
camera.left = -window.innerWidth / 2.0;
camera.right = window.innerWidth / 2.0;
camera.bottom = -window.innerHeight / 2.0;
camera.top = window.innerHeight / 2.0;
camera.updateProjectionMatrix();
}
function animate() {
requestAnimationFrame(animate);
webglRenderer.clear();
if (assetsLoaded) {
textCanvas.render(camera);
}
stats.update();
}
function main() {
// Init Three.JS
webglRenderer = new THREE.WebGLRenderer({
canvas: document.getElementById("mapCanvas") as HTMLCanvasElement
});
webglRenderer.autoClear = false;
webglRenderer.setClearColor(0xffffff);
webglRenderer.setPixelRatio(window.devicePixelRatio);
webglRenderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(webglRenderer.domElement);
document.body.appendChild(stats.dom);
window.addEventListener("resize", onWindowResize);
camera = new THREE.OrthographicCamera(
-window.innerWidth / 2.0,
window.innerWidth / 2.0,
window.innerHeight / 2.0,
-window.innerHeight / 2.0
);
camera.position.z = 1.0;
camera.near = 0.0;
camera.updateProjectionMatrix();
// Init TextCanvas
FontCatalog.load("resources/fonts/Default_FontCatalog.json", 16).then(
(loadedFontCatalog: FontCatalog) => {
textCanvas = new TextCanvas({
renderer: webglRenderer,
fontCatalog: loadedFontCatalog,
maxGlyphCount: 12
});
loadedFontCatalog.loadCharset("0123456789", new TextRenderStyle()).then(() => {
assetsLoaded = true;
addText();
});
}
);
// Animation loop
animate();
}
main();
}