2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
title: Using attributes
|
|
|
|
for: developers
|
|
|
|
about: Show s you have to use attributes on points, paths, and snippets
|
2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
Points, Paths, and Snippets all have [attributes](/reference/api/attributes/) that you can use to
|
|
|
|
influence how they behave.
|
|
|
|
|
|
|
|
A common scenario is to apply CSS classes to style a path:
|
|
|
|
|
|
|
|
```js
|
|
|
|
paths.example.attributes.add('class', 'lining dashed');
|
|
|
|
```
|
|
|
|
|
|
|
|
Because it's so common to set attributes, Points, Paths and Snippets all have
|
2022-02-19 08:04:25 +01:00
|
|
|
the `attr()` helper method.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-02-20 14:35:50 +01:00
|
|
|
Not only is less more, the method is also _chainable_, which allows you to do this:
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
points.message = new Point(0,0)
|
|
|
|
.attr("data-text", "Hello world!")
|
|
|
|
.attr("data-text-class", "note");
|
|
|
|
```
|
|
|
|
|
|
|
|
<Note>
|
|
|
|
|
|
|
|
In this example, we're using attributes to add text to our pattern.
|
2022-04-30 14:07:54 -04:00
|
|
|
The [adding-text](/howtos/code/adding-text) documentation explains this in detail.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
</Note>
|
|
|
|
|
|
|
|
<Tip>
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
When rendering, FreeSewing will output all your attributes. This gives you the
|
2021-08-25 16:09:31 +02:00
|
|
|
possiblity to use any valid attribute to control the appearance.
|
|
|
|
|
2022-02-20 14:35:50 +01:00
|
|
|
This is also why we use the _data-_ prefix for those attributes that have
|
2021-08-25 16:09:31 +02:00
|
|
|
special meaning within FreeSewing, such as `data-text`. Adding a `text` attribute
|
|
|
|
would result in invalid SVG as there is no such thing as a text attribute. But `data-text`
|
2021-10-17 18:26:00 +02:00
|
|
|
is fine because the `data-` prefix indicates it is a [custom attribute](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/data-*).
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
</Tip>
|