Skip to content

Commit d155937

Browse files
hamishwilleebsmth
andauthored
Ff136 SVG discard element follow up (#38433)
* FF136 SVG discard element follow up * Add SVGDiscardElement * Update files/en-us/web/svg/element/discard/index.md Co-authored-by: Brian Smith <[email protected]> --------- Co-authored-by: Brian Smith <[email protected]>
1 parent 2b6564b commit d155937

File tree

6 files changed

+161
-32
lines changed

6 files changed

+161
-32
lines changed

files/en-us/web/api/svg_api/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The SVG API is a set of interfaces that have been categorized into the following
3232
- {{DOMxRef("SVGCursorElement")}}
3333
- {{DOMxRef("SVGDefsElement")}}
3434
- {{DOMxRef("SVGDescElement")}}
35+
- {{DOMxRef("SVGDiscardElement")}}
3536
- {{DOMxRef("SVGElement")}}
3637
- {{DOMxRef("SVGEllipseElement")}}
3738
- {{DOMxRef("SVGFEBlendElement")}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: SVGDiscardElement
3+
slug: Web/API/SVGDiscardElement
4+
page-type: web-api-interface
5+
browser-compat: api.SVGDiscardElement
6+
---
7+
8+
{{APIRef("SVG")}}
9+
10+
The **`SVGDiscardElement`** interface is an interface for the {{SVGElement("discard")}} element.
11+
12+
Note that it does not provide access to the specific attributes of the {{SVGElement("discard")}} element (`begin` and `href`).
13+
14+
{{InheritanceDiagram}}
15+
16+
## Instance properties
17+
18+
_This interface also inherits properties from its parent, {{domxref("SVGAnimationElement")}}._
19+
20+
## Instance methods
21+
22+
_Inherits methods from its parent interface, {{domxref("SVGAnimationElement")}}._
23+
24+
## Specifications
25+
26+
{{Specifications}}
27+
28+
## Browser compatibility
29+
30+
{{Compat}}
31+
32+
## See also
33+
34+
- {{SVGElement("discard")}} SVG element
Loading

files/en-us/web/svg/element/discard/bar_discard.svg files/en-us/web/svg/element/discard/bar_discard_time.svg

+2-2
Loading

files/en-us/web/svg/element/discard/index.md

+111-30
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,48 @@ This allows an SVG viewer to conserve memory by discarding elements that are no
1212

1313
The operation removes the target element and all its children, and then the `<discard>` element itself (this happens even if the target element was invalid).
1414

15-
The [`begin`](#begin) attribute is used to specify the trigger point at which the `<discard>` element becomes active, and its associated element is discarded.
16-
This is commonly an [`offset-value`](/en-US/docs/Web/SVG/Attribute/begin#offset-value) relative to when the SVG file was loaded into the DOM, but it may take other values.
17-
18-
The element that is to be removed from the DOM is specified using the [`href`](#href) attribute.
15+
The [`begin`](#begin) attribute is used to specify the trigger point at which the `<discard>` element becomes active and its associated element is discarded.
16+
The target element that is to be removed from the DOM is specified using the [`href`](#href) attribute.
1917
If not specified, the immediate parent of the `<discard>` element is the target.
2018

2119
`<discard>` may be used in all the same places as the {{SVGElement('animate')}} element.
2220
Authors should set the `playbackorder` attribute to `forwardonly` when using this element, as elements are not re-added if the user seeks backwards in the timeline.
2321

24-
## Example
22+
## Attributes
23+
24+
- {{SVGAttr("begin")}}
25+
26+
- : The trigger that causes the `<discard>` element to become active, at which point the associated element should be discarded.
27+
This is commonly a [`syncbase-value`](/en-US/docs/Web/SVG/Attribute/begin#syncbase-value) indicating the start or end of another animation, an [`offset-value`](/en-US/docs/Web/SVG/Attribute/begin#offset-value) relative to when the SVG file was loaded into the DOM, or an [`event-value`](/en-US/docs/Web/SVG/Attribute/begin#event-value).
28+
29+
_Value type_: [**\<begin-value-list>**](/en-US/docs/Web/SVG/Attribute/begin#animate_animatemotion_animatetransform_set).
30+
_Default value_: `0`; _Animatable_: **no**
31+
32+
- {{SVGAttr("href")}}
33+
34+
- : An URL reference for the target element to discard.
35+
This has the same requirements as [`href` on animation elements](/en-US/docs/Web/SVG/Attribute/href#animate_animatemotion_animatetransform_set), and can be another `<discard>` element.
36+
If not defined, the target element is the immediate parent of the `<discard>` element.
37+
38+
Note that if the target element is not part of the current SVG document fragment, whether or not it is discarded depends on the target language.
39+
40+
_Default value_: `none`; _Animatable_: **no**
41+
42+
## Usage context
43+
44+
{{svginfo}}
45+
46+
## Examples
2547

26-
### Loading bar with discard
48+
### Discard triggered on the end of an animation
2749

2850
This example demonstrates how the `<discard>` element might be used with an activation triggered based on the completion of an animation.
2951
The SVG used is based on Erik Dahlström's "Loading bar" SVG at http://xn--dahlstrm-t4a.net/svg/smil/svgt12_discard.svg.
3052

3153
The SVG defines a "Load complete" {{svgelement("text")}} element that is hidden by a {{svgelement("g")}} element.
3254

33-
The `<rect>` is animated to the end of the bar over a duration of 4 seconds by the animation with id "a".
34-
The `<g>` element contains a discard element that is triggered by completion of the "a" animation: `<discard begin="a.end" />`.
55+
The `<rect>` is animated to the end of the bar over a duration of 4 seconds by the animation with id "barAnim".
56+
The `<g>` element contains a discard element that is triggered by completion of the "barAnim" animation: `<discard begin="barAnim.end" />`.
3557
When this activates the `<g>` element and all its contents are discarded from the DOM, leaving only the text block that displays "Load complete".
3658

3759
```html
@@ -59,7 +81,7 @@ When this activates the `<g>` element and all its contents are discarded from th
5981
fill="white"
6082
stroke="black" />
6183
<rect fill="lime" rx="3" x="12" y="17" height="16.1" width="10">
62-
<animate id="a" attributeName="width" to="76" dur="4s" />
84+
<animate id="barAnim" attributeName="width" to="76" dur="4s" />
6385
</rect>
6486
<text
6587
x="50"
@@ -71,18 +93,17 @@ When this activates the `<g>` element and all its contents are discarded from th
7193
stroke-width="0.2">
7294
Loading...
7395
</text>
74-
<discard begin="a.end" />
96+
<discard begin="barAnim.end" />
7597
</g>
7698
</svg>
7799
```
78100

79101
#### Result
80102

81-
The live example below displays the SVG above in the top image, while the second image is the same SVG file but with the `<discard>` elements removed.
103+
The live example below displays the SVG above in the top image, while the second image is the same SVG file but with the `<discard>` elements removed (on browsers that don't support the discard element both images will behave the same).
82104

83-
On browsers that support the discard element, on the top image, the faster bar will disappear after it reaches the end and is discarded.
84-
When the second bar reaches the end, everything else except the text "Load complete" will disappear as it is discarded.
85-
On browsers that don't support the discard element both images will behave the same: the "Load complete" will never get to display.
105+
On the top image (on browsers that support the `<discard>` element) the bar and everything else in the `<g>` element will disappear after the bar reaches the end and is discarded, leaving only the "Load complete" text.
106+
The lower image progress bar will simply revert to its initial state on completion of the animation, and the "Load complete" text will never get to display.
86107

87108
```html hidden
88109
<button id="reset" type="button">Reset</button>
@@ -97,34 +118,94 @@ reload.addEventListener("click", () => {
97118
```
98119

99120
```html hidden
100-
<img src="bar_discard.svg" alt="Your SVG Image" />
101-
<img src="bar_no_discard.svg" alt="Your SVG Image" />
121+
<img
122+
src="bar_discard_event.svg"
123+
alt="Animated progress bar SVG that uses discard triggered on the end of an animation" />
124+
<img
125+
src="bar_no_discard.svg"
126+
alt="Animated progress bar SVG that does not include discard element" />
102127
```
103128

104129
{{EmbedLiveSample('Result', , '800px')}}
105130

106-
## Attributes
131+
### Discard triggered on time
107132

108-
- {{SVGAttr("begin")}}
133+
This example demonstrates how the `<discard>` element might be used with an activation triggered based on time.
109134

110-
- : The trigger that causes the `<discard>` element to become active, at which point the associated element should be discarded.
111-
This is commonly the time since the SVG was loaded.
112-
_Value type_: [**\<begin-value-list>**](/en-US/docs/Web/SVG/Attribute/begin#animate_animatemotion_animatetransform_set).
113-
_Default value_: `0`; _Animatable_: **no**
135+
It is almost exactly the same as the previous example, the main difference being that the discard element is triggered after 5 seconds (`<discard begin="5s" />`) instead of the the end of the animation (4 seconds). This also removes the id from the `<animate>` element, as it is not used.
114136

115-
- {{SVGAttr("href")}}
137+
```html
138+
<svg
139+
xmlns="http://www.w3.org/2000/svg"
140+
xmlns:xlink="http://www.w3.org/1999/xlink"
141+
viewBox="0 0 100 50">
142+
<text
143+
x="50"
144+
y="30"
145+
font-family="sans-serif"
146+
font-size="10px"
147+
text-anchor="middle"
148+
stroke="white"
149+
stroke-width="0.2">
150+
Load complete
151+
</text>
152+
<g>
153+
<rect
154+
rx="5"
155+
x="10"
156+
y="15"
157+
height="20"
158+
width="80"
159+
fill="white"
160+
stroke="black" />
161+
<rect fill="lime" rx="3" x="12" y="17" height="16.1" width="10">
162+
<animate attributeName="width" to="76" dur="4s" />
163+
</rect>
164+
<text
165+
x="50"
166+
y="30"
167+
font-family="sans-serif"
168+
font-size="10px"
169+
text-anchor="middle"
170+
stroke="white"
171+
stroke-width="0.2">
172+
Loading...
173+
</text>
174+
<discard begin="5s" />
175+
</g>
176+
</svg>
177+
```
116178

117-
- : An URL reference for the target element to discard.
118-
This has the same requirements as [`href` on animation elements](/en-US/docs/Web/SVG/Attribute/href#animate_animatemotion_animatetransform_set), and can be another `<discard>` element.
119-
If not defined, the target element is the immediate parent of the `<discard>` element.
179+
#### Result
120180

121-
Note that if the target element is not part of the current SVG document fragment, whether or not it is discarded depends on the target language.
181+
The live example below displays the SVG above in the top image, while the second image is the same SVG file but with the `<discard>` elements removed (on browsers that don't support the discard element both images will behave the same).
122182

123-
_Default value_: `none`; _Animatable_: **no**
183+
On the top image (on browsers that support the `<discard>` element) the bar will progress to completion after four seconds, and then revert to its initial state.
184+
One second later everything in the `<g>` element will disappear, leaving only the "Load complete" text.
185+
The lower image progress bar will simply revert to its initial state on completion of the animation, and the "Load complete" text will never get to display.
124186

125-
## Usage context
187+
```html hidden
188+
<button id="reset" type="button">Reset</button>
189+
```
126190

127-
{{svginfo}}
191+
```js hidden
192+
const reload = document.querySelector("#reset");
193+
194+
reload.addEventListener("click", () => {
195+
window.location.reload(true);
196+
});
197+
```
198+
199+
```html hidden
200+
<img
201+
src="bar_discard_time.svg"
202+
alt="Animated progress bar SVG that uses discard triggered on time" />
203+
<img
204+
src="bar_no_discard.svg"
205+
alt="Animated progress bar SVG that does not include discard element" />
206+
```
207+
208+
{{EmbedLiveSample('result_2', , '800px')}}
128209

129210
## Specifications
130211

files/jsondata/GroupData.json

+1
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,7 @@
15621562
"SVGCursorElement",
15631563
"SVGDefsElement",
15641564
"SVGDescElement",
1565+
"SVGDiscardElement",
15651566
"SVGElement",
15661567
"SVGEllipseElement",
15671568
"SVGFEBlendElement",

0 commit comments

Comments
 (0)