@lightningjs/blits 1.30.1 → 1.30.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +7 -7
- package/.eslintignore +1 -1
- package/.eslintrc.cjs +65 -65
- package/.github/workflows/comment-lint-results.yml +124 -124
- package/.github/workflows/comment-test-results.yml +108 -108
- package/.github/workflows/deploy-docs.yml +27 -27
- package/.github/workflows/run-blits-tests.yml +141 -141
- package/.github/workflows/run-lint.yml +127 -127
- package/.husky/pre-commit +1 -1
- package/.vscode/extensions.json +7 -7
- package/.vscode/settings.json +9 -9
- package/CHANGELOG.md +1019 -1012
- package/COPYING +1 -0
- package/LICENSE +202 -202
- package/NOTICE +12 -12
- package/README.md +33 -33
- package/bin/index.js +8 -8
- package/blits.d.ts +6 -6
- package/docs/README.md +19 -19
- package/docs/_sidebar.md +37 -37
- package/docs/built-in/directives.md +50 -50
- package/docs/built-in/for-loop.md +203 -203
- package/docs/built-in/layout.md +168 -168
- package/docs/components/component_state.md +90 -90
- package/docs/components/computed_properties.md +45 -45
- package/docs/components/lifecycle_events.md +42 -42
- package/docs/components/methods.md +45 -45
- package/docs/components/props.md +56 -56
- package/docs/components/user_input.md +186 -186
- package/docs/components/utility_methods.md +320 -320
- package/docs/components/watchers.md +34 -34
- package/docs/essentials/application_root.md +19 -19
- package/docs/essentials/components.md +68 -68
- package/docs/essentials/displaying_images.md +67 -67
- package/docs/essentials/displaying_text.md +135 -135
- package/docs/essentials/element_attributes.md +195 -195
- package/docs/essentials/settings.md +117 -117
- package/docs/essentials/template_syntax.md +83 -83
- package/docs/getting_started/file_structure.md +58 -58
- package/docs/getting_started/getting_started.md +45 -45
- package/docs/getting_started/intro.md +20 -20
- package/docs/index.html +47 -47
- package/docs/performance/lazy-loading.md +161 -161
- package/docs/plugins/global_app_state.md +98 -98
- package/docs/plugins/language.md +172 -172
- package/docs/plugins/text-to-speech-announcer.md +119 -119
- package/docs/plugins/theme.md +176 -176
- package/docs/router/basics.md +175 -175
- package/docs/router/hooks.md +70 -70
- package/docs/router/transitions.md +146 -146
- package/docs/sidebar.json +171 -171
- package/docs/transitions_animations/transitions.md +150 -150
- package/index.d.ts +1099 -1099
- package/index.js +28 -28
- package/jsconfig.json +12 -12
- package/package.json +56 -56
- package/public/assets/fps_sprite.base64.js +1 -1
- package/scripts/postpublish.js +59 -59
- package/scripts/prepublishOnly.js +84 -84
- package/scripts/prepush.js +78 -78
- package/src/announcer/announcer.js +162 -162
- package/src/announcer/speechSynthesis.js +107 -107
- package/src/application.js +100 -100
- package/src/component/base/announcer.js +27 -27
- package/src/component/base/events.js +69 -69
- package/src/component/base/index.js +33 -33
- package/src/component/base/methods.js +244 -244
- package/src/component/base/router.js +41 -41
- package/src/component/base/timeouts_intervals.js +99 -99
- package/src/component/base/utils.js +72 -72
- package/src/component/setup/computed.js +48 -48
- package/src/component/setup/index.js +62 -62
- package/src/component/setup/input.js +30 -30
- package/src/component/setup/methods.js +37 -37
- package/src/component/setup/props.js +62 -62
- package/src/component/setup/props.test.js +238 -238
- package/src/component/setup/routes.js +42 -42
- package/src/component/setup/state.js +59 -59
- package/src/component/setup/watch.js +35 -35
- package/src/component.js +437 -437
- package/src/component.test.js +388 -388
- package/src/components/Circle.js +73 -73
- package/src/components/Circle.orig.js +31 -31
- package/src/components/FPScounter.js +1510 -1510
- package/src/components/FPScounter.orig.js +121 -121
- package/src/components/RouterView.js +81 -81
- package/src/components/RouterView.orig.js +54 -54
- package/src/components/Sprite.js +94 -94
- package/src/components/Sprite.orig.js +58 -58
- package/src/components/index.js +28 -28
- package/src/components/index.orig.js +28 -28
- package/src/constants.js +21 -21
- package/src/engine.js +1 -1
- package/src/engines/L3/element.js +744 -744
- package/src/engines/L3/element.test.js +579 -579
- package/src/engines/L3/fontLoader.js +38 -38
- package/src/engines/L3/index.js +7 -7
- package/src/engines/L3/launch.js +123 -123
- package/src/engines/L3/shaderLoader.js +12 -12
- package/src/engines/index.js +1 -1
- package/src/focus.js +90 -90
- package/src/helpers/deepEqualArray.js +50 -50
- package/src/helpers/deepEqualArray.test.js +127 -127
- package/src/helpers/fetchJson.js +34 -34
- package/src/helpers/sequence.js +26 -26
- package/src/launch.js +123 -123
- package/src/lib/blitsfileconverter/blitsfileconverter.js +52 -52
- package/src/lib/codegenerator/generator.js +788 -788
- package/src/lib/codegenerator/generator.test.js +3801 -3801
- package/src/lib/colors/colors.js +92 -92
- package/src/lib/colors/colors.test.js +290 -290
- package/src/lib/colors/htmlColors.js +168 -168
- package/src/lib/componentId.js +49 -49
- package/src/lib/componentId.test.js +84 -84
- package/src/lib/eventListeners.js +123 -123
- package/src/lib/eventListeners.test.js +236 -236
- package/src/lib/hooks.js +59 -59
- package/src/lib/lifecycle.js +85 -85
- package/src/lib/log.js +117 -117
- package/src/lib/precompiler/precompiler.js +73 -73
- package/src/lib/reactivity/effect.js +93 -93
- package/src/lib/reactivity/effect.test.js +35 -35
- package/src/lib/reactivity/reactive.js +208 -208
- package/src/lib/reactivity/reactive.test.js +27 -27
- package/src/lib/reactivityguard/computedprops.js +218 -218
- package/src/lib/symbols.js +108 -108
- package/src/lib/templateparser/parser.js +386 -386
- package/src/lib/templateparser/parser.test.js +1411 -1411
- package/src/lib/utils.js +27 -27
- package/src/plugin.js +27 -27
- package/src/plugins/appstate.js +26 -26
- package/src/plugins/index.js +20 -20
- package/src/plugins/language.js +152 -152
- package/src/plugins/log.js +25 -25
- package/src/plugins/theme.js +94 -94
- package/src/router/router.js +514 -514
- package/src/router/router.test.js +473 -473
- package/src/router/transitions/fadeIn.js +27 -27
- package/src/router/transitions/fadeInOut.js +33 -33
- package/src/router/transitions/index.js +21 -21
- package/src/router/transitions/slideIn.js +28 -28
- package/src/router/transitions/slideInOut.js +33 -33
- package/src/settings.js +40 -40
- package/vite/blitsFileConverter.js +67 -67
- package/vite/index.js +28 -28
- package/vite/msdfGenerator.js +269 -269
- package/vite/preCompiler.js +42 -42
- package/vite/reactivityGuard.js +45 -45
- package/vscode/data/template-attributes.json +589 -589
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
# Watchers
|
|
2
|
-
|
|
3
|
-
In some cases, you may want to execute specific actions whenever the value of a state variable, a prop, or a computed property changes. These actions could involve dispatching an event or updating another state variable.
|
|
4
|
-
|
|
5
|
-
You might be tempted to handle this functionality inside a computed property, but this is not recommended. Computed properties should not have side effects, to prevent the risk of falling into an endless loop.
|
|
6
|
-
|
|
7
|
-
Instead, Blits allows you to specify **watchers** to trigger functionality when certain variables change.
|
|
8
|
-
|
|
9
|
-
## Using Watchers
|
|
10
|
-
|
|
11
|
-
Within the `watch` key of the _Component configuration object_, you can define an object of _watcher functions_.
|
|
12
|
-
|
|
13
|
-
The name of each function should correspond with the name of the state variable, the prop, or the computed property that you want to observe. Whenever the value of the observed target changes, the respective watcher function will be invoked.
|
|
14
|
-
|
|
15
|
-
The watcher function receives two arguments: the _new value_ and the _old value_ of the observed property. This allows you to perform specific actions based on the changes.
|
|
16
|
-
|
|
17
|
-
```javascript
|
|
18
|
-
{
|
|
19
|
-
state() {
|
|
20
|
-
return {
|
|
21
|
-
alpha: 0.2
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
watch: {
|
|
25
|
-
alpha(value, oldValue) {
|
|
26
|
-
if(value > oldValue) {
|
|
27
|
-
// Execute some logic when the 'alpha' value increases
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
In this example, whenever the value of state variable `alpha` changes, the `alpha` watcher function will be invoked. The function checks if the new value is greater than the old value and executes custom logic accordingly.
|
|
1
|
+
# Watchers
|
|
2
|
+
|
|
3
|
+
In some cases, you may want to execute specific actions whenever the value of a state variable, a prop, or a computed property changes. These actions could involve dispatching an event or updating another state variable.
|
|
4
|
+
|
|
5
|
+
You might be tempted to handle this functionality inside a computed property, but this is not recommended. Computed properties should not have side effects, to prevent the risk of falling into an endless loop.
|
|
6
|
+
|
|
7
|
+
Instead, Blits allows you to specify **watchers** to trigger functionality when certain variables change.
|
|
8
|
+
|
|
9
|
+
## Using Watchers
|
|
10
|
+
|
|
11
|
+
Within the `watch` key of the _Component configuration object_, you can define an object of _watcher functions_.
|
|
12
|
+
|
|
13
|
+
The name of each function should correspond with the name of the state variable, the prop, or the computed property that you want to observe. Whenever the value of the observed target changes, the respective watcher function will be invoked.
|
|
14
|
+
|
|
15
|
+
The watcher function receives two arguments: the _new value_ and the _old value_ of the observed property. This allows you to perform specific actions based on the changes.
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
{
|
|
19
|
+
state() {
|
|
20
|
+
return {
|
|
21
|
+
alpha: 0.2
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
watch: {
|
|
25
|
+
alpha(value, oldValue) {
|
|
26
|
+
if(value > oldValue) {
|
|
27
|
+
// Execute some logic when the 'alpha' value increases
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
In this example, whenever the value of state variable `alpha` changes, the `alpha` watcher function will be invoked. The function checks if the new value is greater than the old value and executes custom logic accordingly.
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
# Application root
|
|
2
|
-
|
|
3
|
-
Every Blits App starts with a base Application component.
|
|
4
|
-
|
|
5
|
-
Ultimately this Application is a component like any regular Blits component. But it is augmented with some extra functionality. `Blits.Application` is responsible for setting up the listeners for keyhandling for example.
|
|
6
|
-
|
|
7
|
-
You can only have 1 Application component per App. By default, this file is named `App.js` and it is placed in the root of the `src`-folder.
|
|
8
|
-
|
|
9
|
-
`src/App.js` will look something like this:
|
|
10
|
-
|
|
11
|
-
```js
|
|
12
|
-
import Blits from '@lightningjs/blits'
|
|
13
|
-
|
|
14
|
-
export default Blits.Application({
|
|
15
|
-
template: `
|
|
16
|
-
<Element></Element>
|
|
17
|
-
`,
|
|
18
|
-
})
|
|
19
|
-
```
|
|
1
|
+
# Application root
|
|
2
|
+
|
|
3
|
+
Every Blits App starts with a base Application component.
|
|
4
|
+
|
|
5
|
+
Ultimately this Application is a component like any regular Blits component. But it is augmented with some extra functionality. `Blits.Application` is responsible for setting up the listeners for keyhandling for example.
|
|
6
|
+
|
|
7
|
+
You can only have 1 Application component per App. By default, this file is named `App.js` and it is placed in the root of the `src`-folder.
|
|
8
|
+
|
|
9
|
+
`src/App.js` will look something like this:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import Blits from '@lightningjs/blits'
|
|
13
|
+
|
|
14
|
+
export default Blits.Application({
|
|
15
|
+
template: `
|
|
16
|
+
<Element></Element>
|
|
17
|
+
`,
|
|
18
|
+
})
|
|
19
|
+
```
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
# Components
|
|
2
|
-
|
|
3
|
-
Like any modern front-end framework, Blits is built around the concept of Components. Components allow you to split the UI of your App into
|
|
4
|
-
reusable pieces of code and logic.
|
|
5
|
-
|
|
6
|
-
As you get started building your App, you're free to put all your elements and logic right in the root Application component.
|
|
7
|
-
This is great for quickly playing around with Blits and Lightning.
|
|
8
|
-
|
|
9
|
-
But soon you'll find that it's beneficial to start organizing the different parts of your App into separate components. Blits Components can be nested into a tree of components.
|
|
10
|
-
|
|
11
|
-
Components in Blits follow a clearly defined structure, which helps keep your code clean and guides you to quickly build your components.
|
|
12
|
-
|
|
13
|
-
This universal structure is also valuable for creating drop-in components that can be easily shared across projects.
|
|
14
|
-
|
|
15
|
-
## Creating a new Component
|
|
16
|
-
|
|
17
|
-
Let's see how to create a new component and explore the basic anatomy of a component.
|
|
18
|
-
|
|
19
|
-
A new component is created using the `Blits.Component()` function that is exported from the `@lightningjs/blits` package. This function accepts two arguments:
|
|
20
|
-
|
|
21
|
-
- The first argument is the _name_ of the component. This name will be used in debug log messages, so make sure to choose a unique and descriptive name for your component, such as `Homepage`, `Loader`, or `SidebarMenuItem`.
|
|
22
|
-
|
|
23
|
-
- The second argument is a _Component configuration object_, represented as an `object literal`. This Component configuration object can contain a predefined set of key-value pairs, that define how your Component looks and behaves.
|
|
24
|
-
|
|
25
|
-
Some of the commonly used Component configuration options include:
|
|
26
|
-
|
|
27
|
-
- `template`: to define the template for the component.
|
|
28
|
-
- `state`: to specify the component instance's internal state.
|
|
29
|
-
- `props`: to define the props that can be passed into the component.
|
|
30
|
-
- `hooks`: allowing you to hook into different lifecycle events of the component.
|
|
31
|
-
- `input`: used to tap into user input.
|
|
32
|
-
- `methods`: used to define more complex functionality of the component
|
|
33
|
-
|
|
34
|
-
A basic Blits component will then look something like this:
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
// src/components/Loader.js
|
|
38
|
-
export default Blits.Component('Loader', {
|
|
39
|
-
template: `
|
|
40
|
-
<Element x="880" y="500" :show="$active">
|
|
41
|
-
<Element x="$offset" w="40" h="40" color="#94a3b8" :alpha="$alpha" />
|
|
42
|
-
</Element>
|
|
43
|
-
`,
|
|
44
|
-
props: ['type', 'size'],
|
|
45
|
-
state() {
|
|
46
|
-
return {
|
|
47
|
-
active: false,
|
|
48
|
-
alpha: 0.4,
|
|
49
|
-
offset: 100
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
hooks: {
|
|
53
|
-
ready() {
|
|
54
|
-
this.$setTimeout(() => {
|
|
55
|
-
this.active = true
|
|
56
|
-
}, 300)
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
input: {
|
|
60
|
-
enter() {
|
|
61
|
-
this.alpha = 1
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
In the next sections, we will go into the details of each of the component configuration options.
|
|
68
|
-
|
|
1
|
+
# Components
|
|
2
|
+
|
|
3
|
+
Like any modern front-end framework, Blits is built around the concept of Components. Components allow you to split the UI of your App into
|
|
4
|
+
reusable pieces of code and logic.
|
|
5
|
+
|
|
6
|
+
As you get started building your App, you're free to put all your elements and logic right in the root Application component.
|
|
7
|
+
This is great for quickly playing around with Blits and Lightning.
|
|
8
|
+
|
|
9
|
+
But soon you'll find that it's beneficial to start organizing the different parts of your App into separate components. Blits Components can be nested into a tree of components.
|
|
10
|
+
|
|
11
|
+
Components in Blits follow a clearly defined structure, which helps keep your code clean and guides you to quickly build your components.
|
|
12
|
+
|
|
13
|
+
This universal structure is also valuable for creating drop-in components that can be easily shared across projects.
|
|
14
|
+
|
|
15
|
+
## Creating a new Component
|
|
16
|
+
|
|
17
|
+
Let's see how to create a new component and explore the basic anatomy of a component.
|
|
18
|
+
|
|
19
|
+
A new component is created using the `Blits.Component()` function that is exported from the `@lightningjs/blits` package. This function accepts two arguments:
|
|
20
|
+
|
|
21
|
+
- The first argument is the _name_ of the component. This name will be used in debug log messages, so make sure to choose a unique and descriptive name for your component, such as `Homepage`, `Loader`, or `SidebarMenuItem`.
|
|
22
|
+
|
|
23
|
+
- The second argument is a _Component configuration object_, represented as an `object literal`. This Component configuration object can contain a predefined set of key-value pairs, that define how your Component looks and behaves.
|
|
24
|
+
|
|
25
|
+
Some of the commonly used Component configuration options include:
|
|
26
|
+
|
|
27
|
+
- `template`: to define the template for the component.
|
|
28
|
+
- `state`: to specify the component instance's internal state.
|
|
29
|
+
- `props`: to define the props that can be passed into the component.
|
|
30
|
+
- `hooks`: allowing you to hook into different lifecycle events of the component.
|
|
31
|
+
- `input`: used to tap into user input.
|
|
32
|
+
- `methods`: used to define more complex functionality of the component
|
|
33
|
+
|
|
34
|
+
A basic Blits component will then look something like this:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
// src/components/Loader.js
|
|
38
|
+
export default Blits.Component('Loader', {
|
|
39
|
+
template: `
|
|
40
|
+
<Element x="880" y="500" :show="$active">
|
|
41
|
+
<Element x="$offset" w="40" h="40" color="#94a3b8" :alpha="$alpha" />
|
|
42
|
+
</Element>
|
|
43
|
+
`,
|
|
44
|
+
props: ['type', 'size'],
|
|
45
|
+
state() {
|
|
46
|
+
return {
|
|
47
|
+
active: false,
|
|
48
|
+
alpha: 0.4,
|
|
49
|
+
offset: 100
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
hooks: {
|
|
53
|
+
ready() {
|
|
54
|
+
this.$setTimeout(() => {
|
|
55
|
+
this.active = true
|
|
56
|
+
}, 300)
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
input: {
|
|
60
|
+
enter() {
|
|
61
|
+
this.alpha = 1
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
In the next sections, we will go into the details of each of the component configuration options.
|
|
68
|
+
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
# Displaying Images
|
|
2
|
-
|
|
3
|
-
Now that we've covered the basics of creating Elements, as well as styling and positioning them, it's time to move away from these boring rectangles and explore how you can incorporate _images_ into your App.
|
|
4
|
-
|
|
5
|
-
In Blits, it is very easy to display an image. Simply add the `src` attribute to an Element, specifying the image's location.
|
|
6
|
-
|
|
7
|
-
For local images, make sure to place them in the `public` folder of your App (e.g., `public/assets/background.jpg` or `public/images/logo.png`) and refer to them with a _relative_ path (but omitting the `public` folder as root).
|
|
8
|
-
|
|
9
|
-
Remote images are also supported and can be linked directly (e.g., `http://mycdn.com/artists/jimi_hendrix/woodstock.jpg`).
|
|
10
|
-
|
|
11
|
-
```xml
|
|
12
|
-
<Element src="images/logo.png" w="100" h="80" />
|
|
13
|
-
<Element src="http://mycdn.com/artists/jimi_hendrix/woodstock.jpg" w="1920" h="1080" />
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## Sizing and Scaling
|
|
17
|
-
|
|
18
|
-
Make sure to give your Element a width (`w` ) and a height (`h`) attribute. Images will _not_ be rendered if they don't have both attributes present. The Lightning renderer will scale the image to fit these exact dimensions.
|
|
19
|
-
|
|
20
|
-
For the best performance, it's important to keep your source images as small as possible. If you're displaying an image at `200px x 200px`, make sure the image is exactly that size or _smaller_. The latter option may lead to some quality loss, but can positively impact the overall performance of your App.
|
|
21
|
-
|
|
22
|
-
## Colorization
|
|
23
|
-
|
|
24
|
-
You also have the option to _colorize_ an image on the fly. Just add a `color` attribute to the Element with a `src` attribute. You can use a single color, or apply a gradient.
|
|
25
|
-
|
|
26
|
-
By default, all Elements with a `src` attribute get a solid white background, with the result that the actual colors of the image will be shown.
|
|
27
|
-
|
|
28
|
-
```xml
|
|
29
|
-
<Element
|
|
30
|
-
w="200"
|
|
31
|
-
h="300"
|
|
32
|
-
:src="$src"
|
|
33
|
-
color="{top: '#fff', bottom: '#000'}"
|
|
34
|
-
/>
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Asynchronous Loading
|
|
38
|
-
|
|
39
|
-
All images are loaded asynchronously (and can possibly fail to load), even those local to your App. Blits allows you to easily hook into the `loaded` and `error` events of image Elements.
|
|
40
|
-
|
|
41
|
-
You can use this, for example, to only make something visible once an image is fully loaded. Or to display a fallback image when a remote image can't be retrieved.
|
|
42
|
-
|
|
43
|
-
```xml
|
|
44
|
-
<Element src="http://mycdn.com/lightning.jpg" @loaded="$revealPage" @error="$showFallback" />
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
> Note how events are prefixed with a `@` sign.
|
|
48
|
-
|
|
49
|
-
Considering the template above you would do something like the following in the `methods` key of your Component configuration object:
|
|
50
|
-
|
|
51
|
-
```js
|
|
52
|
-
{
|
|
53
|
-
//...
|
|
54
|
-
methods: {
|
|
55
|
-
revealPage(dimensions) {
|
|
56
|
-
this.$log.info('Image dimensions', dimensions.w, dimensions.h)
|
|
57
|
-
this.show = true
|
|
58
|
-
},
|
|
59
|
-
showFallback(error) {
|
|
60
|
-
this.$log.error('Image failed to load', error)
|
|
61
|
-
this.showBackupImage()
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
The `loaded` event receives image dimensions as its argument and the `error` event receives an error message explaining the failure.
|
|
1
|
+
# Displaying Images
|
|
2
|
+
|
|
3
|
+
Now that we've covered the basics of creating Elements, as well as styling and positioning them, it's time to move away from these boring rectangles and explore how you can incorporate _images_ into your App.
|
|
4
|
+
|
|
5
|
+
In Blits, it is very easy to display an image. Simply add the `src` attribute to an Element, specifying the image's location.
|
|
6
|
+
|
|
7
|
+
For local images, make sure to place them in the `public` folder of your App (e.g., `public/assets/background.jpg` or `public/images/logo.png`) and refer to them with a _relative_ path (but omitting the `public` folder as root).
|
|
8
|
+
|
|
9
|
+
Remote images are also supported and can be linked directly (e.g., `http://mycdn.com/artists/jimi_hendrix/woodstock.jpg`).
|
|
10
|
+
|
|
11
|
+
```xml
|
|
12
|
+
<Element src="images/logo.png" w="100" h="80" />
|
|
13
|
+
<Element src="http://mycdn.com/artists/jimi_hendrix/woodstock.jpg" w="1920" h="1080" />
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Sizing and Scaling
|
|
17
|
+
|
|
18
|
+
Make sure to give your Element a width (`w` ) and a height (`h`) attribute. Images will _not_ be rendered if they don't have both attributes present. The Lightning renderer will scale the image to fit these exact dimensions.
|
|
19
|
+
|
|
20
|
+
For the best performance, it's important to keep your source images as small as possible. If you're displaying an image at `200px x 200px`, make sure the image is exactly that size or _smaller_. The latter option may lead to some quality loss, but can positively impact the overall performance of your App.
|
|
21
|
+
|
|
22
|
+
## Colorization
|
|
23
|
+
|
|
24
|
+
You also have the option to _colorize_ an image on the fly. Just add a `color` attribute to the Element with a `src` attribute. You can use a single color, or apply a gradient.
|
|
25
|
+
|
|
26
|
+
By default, all Elements with a `src` attribute get a solid white background, with the result that the actual colors of the image will be shown.
|
|
27
|
+
|
|
28
|
+
```xml
|
|
29
|
+
<Element
|
|
30
|
+
w="200"
|
|
31
|
+
h="300"
|
|
32
|
+
:src="$src"
|
|
33
|
+
color="{top: '#fff', bottom: '#000'}"
|
|
34
|
+
/>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Asynchronous Loading
|
|
38
|
+
|
|
39
|
+
All images are loaded asynchronously (and can possibly fail to load), even those local to your App. Blits allows you to easily hook into the `loaded` and `error` events of image Elements.
|
|
40
|
+
|
|
41
|
+
You can use this, for example, to only make something visible once an image is fully loaded. Or to display a fallback image when a remote image can't be retrieved.
|
|
42
|
+
|
|
43
|
+
```xml
|
|
44
|
+
<Element src="http://mycdn.com/lightning.jpg" @loaded="$revealPage" @error="$showFallback" />
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
> Note how events are prefixed with a `@` sign.
|
|
48
|
+
|
|
49
|
+
Considering the template above you would do something like the following in the `methods` key of your Component configuration object:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
{
|
|
53
|
+
//...
|
|
54
|
+
methods: {
|
|
55
|
+
revealPage(dimensions) {
|
|
56
|
+
this.$log.info('Image dimensions', dimensions.w, dimensions.h)
|
|
57
|
+
this.show = true
|
|
58
|
+
},
|
|
59
|
+
showFallback(error) {
|
|
60
|
+
this.$log.error('Image failed to load', error)
|
|
61
|
+
this.showBackupImage()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The `loaded` event receives image dimensions as its argument and the `error` event receives an error message explaining the failure.
|