aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-03-28 13:52:57 +1000
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-03-28 13:52:57 +1000
commitba2948bfad00906ffa3ba5ea7ff15ac0be517445 (patch)
tree953b7ebfef53b8a3e594632d9d2b7458028c7170 /src/utils
parent9beddac36db60c0e4097e01272b6faa04bdb1065 (diff)
downloadsvg.js-ba2948bfad00906ffa3ba5ea7ff15ac0be517445.tar.gz
svg.js-ba2948bfad00906ffa3ba5ea7ff15ac0be517445.zip
This is a big one...
### Fixed - fixed `zoom()` method of runner which was passed a wrong parameter - fixed positioning methods of `TSpan` to position them by its bounding box - fixed `flip()` method which flips correctly by center by default now and accepts correct arguments - fixed a case in `rbox()` where not always all values of the box were updated - fixed `getOrigin()` function used by `transform()` so that all origin (#1085) popssibilities specified in the docs are working - fixed positioning of text by its baseline when using `amove()` - fixed tons of typings in the svg.d.ts file ### Added - added second Parameter to `SVG(el, isHTML)` which allows to explicitely create elements in the HTML namespace (#1058) - added `unlink()` and `linker()` to hyperlinked elements to remove or access the underling `<a>` element - added `wrap()` method to `Dom` which lets you wrap an element by another one - added `orient()` method to `Marker` - added `options` parameter to `dispatch()` and `fire()` to allow for more special needs - added `newLine()` constructor to `Text` to create a tspan marked as new line (#1088) - added lots of tests in es6 format
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/adopter.js5
-rw-r--r--src/utils/utils.js61
2 files changed, 41 insertions, 25 deletions
diff --git a/src/utils/adopter.js b/src/utils/adopter.js
index e8b30ea..30eab84 100644
--- a/src/utils/adopter.js
+++ b/src/utils/adopter.js
@@ -13,7 +13,7 @@ export function create (name) {
return globals.document.createElementNS(ns, name)
}
-export function makeInstance (element) {
+export function makeInstance (element, isHTML = false) {
if (element instanceof Base) return element
if (typeof element === 'object') {
@@ -28,7 +28,8 @@ export function makeInstance (element) {
return adopter(globals.document.querySelector(element))
}
- var node = create('svg')
+ // Make sure, that HTML elements are created with the correct namespace
+ var node = isHTML ? globals.document.createElement('div') : create('svg')
node.innerHTML = element
// We can use firstChild here because we know,
diff --git a/src/utils/utils.js b/src/utils/utils.js
index 5d8706e..ee47079 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -43,7 +43,7 @@ export function camelCase (s) {
})
}
-// Convert camel cased string to string seperated
+// Convert camel cased string to dash seperated
export function unCamelCase (s) {
return s.replace(/([A-Z])/g, function (m, g) {
return '-' + g.toLowerCase()
@@ -73,31 +73,46 @@ export function proportionalSize (element, width, height, box) {
}
}
+/**
+ * This function adds support for string origins.
+ * It searches for an origin in o.origin o.ox and o.originX.
+ * This way, origin: {x: 'center', y: 50} can be passed as well as ox: 'center', oy: 50
+**/
export function getOrigin (o, element) {
- // Allow origin or around as the names
- const origin = o.origin // o.around == null ? o.origin : o.around
- let ox, oy
-
- // Allow the user to pass a string to rotate around a given point
- if (typeof origin === 'string' || origin == null) {
- // Get the bounding box of the element with no transformations applied
- const string = (origin || 'center').toLowerCase().trim()
+ const origin = o.origin
+ // First check if origin is in ox or originX
+ let ox = o.ox != null ? o.ox
+ : o.originX != null ? o.originX
+ : 'center'
+ let oy = o.oy != null ? o.oy
+ : o.originY != null ? o.originY
+ : 'center'
+
+ // Then check if origin was used and overwrite in that case
+ if (origin != null) {
+ [ ox, oy ] = Array.isArray(origin) ? origin
+ : typeof origin === 'object' ? [ origin.x, origin.y ]
+ : [ origin, origin ]
+ }
+
+ // Make sure to only call bbox when actually needed
+ const condX = typeof ox === 'string'
+ const condY = typeof oy === 'string'
+ if (condX || condY) {
const { height, width, x, y } = element.bbox()
- // Calculate the transformed x and y coordinates
- const bx = string.includes('left') ? x
- : string.includes('right') ? x + width
- : x + width / 2
- const by = string.includes('top') ? y
- : string.includes('bottom') ? y + height
- : y + height / 2
-
- // Set the bounds eg : "bottom-left", "Top right", "middle" etc...
- ox = o.ox != null ? o.ox : bx
- oy = o.oy != null ? o.oy : by
- } else {
- ox = origin[0]
- oy = origin[1]
+ // And only overwrite if string was passed for this specific axis
+ if (condX) {
+ ox = ox.includes('left') ? x
+ : ox.includes('right') ? x + width
+ : x + width / 2
+ }
+
+ if (condY) {
+ oy = oy.includes('top') ? y
+ : oy.includes('bottom') ? y + height
+ : y + height / 2
+ }
}
// Return the origin as it is if it wasn't a string