master
yutent 2024-03-25 16:34:14 +08:00
parent 18bd4d1df2
commit f5a4709f14
5 changed files with 57 additions and 59 deletions

View File

@ -561,42 +561,35 @@ elproto.clone = function () {
clone.insertAfter(this) clone.insertAfter(this)
return clone return clone
} }
/*\ /**
* SnapElement.toDefs * Moves element to the shared `<defs>` area
[ method ] **
** */
* Moves element to the shared `<defs>` area
**
= (SnapElement) the element
\*/
elproto.toDefs = function () { elproto.toDefs = function () {
let defs = getSomeDefs(this) let defs = getSomeDefs(this)
defs.appendChild(this.node) defs.appendChild(this.node)
return this return this
} }
/*\ /**
* SnapElement.toPattern * Creates a `<pattern>` element from the current element
[ method ] **
** * To create a pattern you have to specify the pattern rect:
* Creates a `<pattern>` element from the current element - x (string|number)
** - y (string|number)
* To create a pattern you have to specify the pattern rect: - width (string|number)
- x (string|number) - height (string|number)
- y (string|number) = (SnapElement) the `<pattern>` element
- width (string|number) * You can use pattern later on as an argument for `fill` attribute:
- height (string|number) let p = paper.path("M10-5-10,15M15,0,0,15M0-5-20,15").attr({
= (SnapElement) the `<pattern>` element fill: "none",
* You can use pattern later on as an argument for `fill` attribute: stroke: "#bada55",
| let p = paper.path("M10-5-10,15M15,0,0,15M0-5-20,15").attr({ strokeWidth: 5
| fill: "none", }).pattern(0, 0, 10, 10),
| stroke: "#bada55", c = paper.circle(200, 200, 100);
| strokeWidth: 5 c.attr({
| }).pattern(0, 0, 10, 10), fill: p
| c = paper.circle(200, 200, 100); });
| c.attr({ */
| fill: p
| });
\*/
elproto.pattern = elproto.toPattern = function (x, y, width, height) { elproto.pattern = elproto.toPattern = function (x, y, width, height) {
let p = make('pattern', getSomeDefs(this)) let p = make('pattern', getSomeDefs(this))
if (x == null) { if (x == null) {
@ -620,25 +613,19 @@ elproto.pattern = elproto.toPattern = function (x, y, width, height) {
p.node.appendChild(this.node) p.node.appendChild(this.node)
return p return p
} }
// SIERRA SnapElement.marker(): clarify what a reference point is. E.g., helps you offset the object from its edge such as when centering it over a path.
// SIERRA SnapElement.marker(): I suggest the method should accept default reference point values. Perhaps centered with (refX = width/2) and (refY = height/2)? Also, couldn't it assume the element's current _width_ and _height_? And please specify what _x_ and _y_ mean: offsets? If so, from where? Couldn't they also be assigned default values? /**
/*\ * Creates a `<marker>` element from the current element
* SnapElement.marker **
[ method ] * To create a marker you have to specify the bounding rect and reference point:
** * @param x (number)
* Creates a `<marker>` element from the current element * @param y (number)
** * @param width (number)
* To create a marker you have to specify the bounding rect and reference point: * @param height (number)
- x (number) * @param refX (number)
- y (number) * @param refY (number)
- width (number) * You can specify the marker later as an argument for `marker-start`, `marker-end`, `marker-mid`, and `marker` attributes. The `marker` attribute places the marker at every point along the path, and `marker-mid` places them at every point except the start and end.
- height (number) */
- refX (number)
- refY (number)
= (SnapElement) the `<marker>` element
* You can specify the marker later as an argument for `marker-start`, `marker-end`, `marker-mid`, and `marker` attributes. The `marker` attribute places the marker at every point along the path, and `marker-mid` places them at every point except the start and end.
\*/
// TODO add usage for markers
elproto.marker = function (x, y, width, height, refX, refY) { elproto.marker = function (x, y, width, height, refX, refY) {
let p = make('marker', getSomeDefs(this)) let p = make('marker', getSomeDefs(this))
if (x == null) { if (x == null) {

View File

@ -8,6 +8,9 @@ export const doc = document
export const win = window export const win = window
export const xlink = 'http://www.w3.org/1999/xlink' export const xlink = 'http://www.w3.org/1999/xlink'
export const xmlns = 'http://www.w3.org/2000/svg' export const xmlns = 'http://www.w3.org/2000/svg'
export const xhtmlns = 'http://www.w3.org/1999/xhtml'
export const HTML_TAGS = ['div', 'span', 'p']
export const CSS_ATTR = { export const CSS_ATTR = {
'alignment-baseline': 1, 'alignment-baseline': 1,

View File

@ -4,7 +4,6 @@
* @date 2024/03/07 14:41:43 * @date 2024/03/07 14:41:43
*/ */
import { is } from './utils.js'
import { rad, deg } from './lib/math.js' import { rad, deg } from './lib/math.js'
function norm(a) { function norm(a) {

View File

@ -644,7 +644,7 @@ export class Paper {
dom = width dom = width
} }
if (dom) { if (dom) {
let doc = dom.ownerDocument h(dom, { xmlns })
res = new SnapElement(dom) res = new SnapElement(dom)
desc = dom.querySelector('desc') desc = dom.querySelector('desc')
defs = dom.querySelector('defs') defs = dom.querySelector('defs')
@ -779,6 +779,10 @@ export class Paper {
text(x = 0, y = 0, text = '') { text(x = 0, y = 0, text = '') {
return this.el('text', { x, y }, text) return this.el('text', { x, y }, text)
} }
autoText(x = 0, y = 0, text = '') {
return this.el('foreignObject', { x, y, width: 200, height: 40 }, text)
}
/*** /***
* Draws a line * Draws a line
** **
@ -1071,7 +1075,7 @@ export class Paper {
res res
f.appendChild(d) f.appendChild(d)
d.appendChild(svg) d.appendChild(svg)
h(svg, { xmlns: 'http://www.w3.org/2000/svg' }) h(svg, { xmlns })
res = d.innerHTML res = d.innerHTML
return res return res
} }

View File

@ -4,7 +4,7 @@
* @date 2024/03/06 09:55:31 * @date 2024/03/06 09:55:31
*/ */
import { xlink, xmlns, doc, win } from './lib/constants.js' import { xlink, xmlns, xhtmlns, HTML_TAGS, doc, win } from './lib/constants.js'
export function uuid(prefix = '') { export function uuid(prefix = '') {
return prefix + Math.random().toString(16).slice(-8) return prefix + Math.random().toString(16).slice(-8)
@ -80,7 +80,7 @@ export function $(el, attr) {
export function h(el, props = null, children) { export function h(el, props = null, children) {
if (typeof el === 'string') { if (typeof el === 'string') {
el = doc.createElementNS(xmlns, el) el = doc.createElementNS(HTML_TAGS.includes(el) ? xhtmlns : xmlns, el)
} }
if (props) { if (props) {
@ -103,10 +103,15 @@ export function h(el, props = null, children) {
if (children) { if (children) {
if (Array.isArray(children)) { if (Array.isArray(children)) {
let f = doc.createDocumentFragment() children = children.join('')
for (let i = -1, it; (it = children[++i]); ) { }
f.appendChild(h('tspan', { dy: i * 16 }, it))
} if (el.tagName.toLowerCase() === 'foreignobject') {
let f = h('div', { xmlns: 'http://www.w3.org/1999/xhtml' })
let p = h('p', null, children)
f.appendChild(p)
h(el, { xmlns })
el.appendChild(f) el.appendChild(f)
} else { } else {
el.appendChild(doc.createTextNode(children)) el.appendChild(doc.createTextNode(children))