How to use svg.js's transform with matrix parameter in typescript?
When I use svg.js lib with javascript, I can transform a given element via a SVG.Matrix
like so:
import * as SVG from 'svg.js';
let draw = SVG('drawing').size(200, 200);
let rect = draw.rect(100, 100).attr({ fill: '#f06' });
rect.translate(100,100);
let matrix = new SVG.Matrix(rect);
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(matrix);
In the example the 2nd rect gets transformed like the first rect via SVG.Matrix
:
<svg id="SvgjsSvg1001" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="100" height="100" fill="#ff0066" transform="matrix(1,0,0,1,100,100)"></rect><rect id="SvgjsRect1009" width="100" height="100" fill="#ff6600" transform="matrix(1,0,0,1,100,100)"></rect></svg>
However the same code in typescript claims:
[ts]
Argument of type 'Matrix' is not assignable to parameter of type 'Transform'.
Types of property 'skewX' are incompatible.
Type '(x: number, cx?: number, cy?: number) => Matrix' is not assignable to type 'number'.
How can I transform an element via a SVG.Matrix
in typescript?
Why does svgjs.Element.transform
behave different when used with ts?
typescript svg matrix svg.js
add a comment |
When I use svg.js lib with javascript, I can transform a given element via a SVG.Matrix
like so:
import * as SVG from 'svg.js';
let draw = SVG('drawing').size(200, 200);
let rect = draw.rect(100, 100).attr({ fill: '#f06' });
rect.translate(100,100);
let matrix = new SVG.Matrix(rect);
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(matrix);
In the example the 2nd rect gets transformed like the first rect via SVG.Matrix
:
<svg id="SvgjsSvg1001" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="100" height="100" fill="#ff0066" transform="matrix(1,0,0,1,100,100)"></rect><rect id="SvgjsRect1009" width="100" height="100" fill="#ff6600" transform="matrix(1,0,0,1,100,100)"></rect></svg>
However the same code in typescript claims:
[ts]
Argument of type 'Matrix' is not assignable to parameter of type 'Transform'.
Types of property 'skewX' are incompatible.
Type '(x: number, cx?: number, cy?: number) => Matrix' is not assignable to type 'number'.
How can I transform an element via a SVG.Matrix
in typescript?
Why does svgjs.Element.transform
behave different when used with ts?
typescript svg matrix svg.js
add a comment |
When I use svg.js lib with javascript, I can transform a given element via a SVG.Matrix
like so:
import * as SVG from 'svg.js';
let draw = SVG('drawing').size(200, 200);
let rect = draw.rect(100, 100).attr({ fill: '#f06' });
rect.translate(100,100);
let matrix = new SVG.Matrix(rect);
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(matrix);
In the example the 2nd rect gets transformed like the first rect via SVG.Matrix
:
<svg id="SvgjsSvg1001" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="100" height="100" fill="#ff0066" transform="matrix(1,0,0,1,100,100)"></rect><rect id="SvgjsRect1009" width="100" height="100" fill="#ff6600" transform="matrix(1,0,0,1,100,100)"></rect></svg>
However the same code in typescript claims:
[ts]
Argument of type 'Matrix' is not assignable to parameter of type 'Transform'.
Types of property 'skewX' are incompatible.
Type '(x: number, cx?: number, cy?: number) => Matrix' is not assignable to type 'number'.
How can I transform an element via a SVG.Matrix
in typescript?
Why does svgjs.Element.transform
behave different when used with ts?
typescript svg matrix svg.js
When I use svg.js lib with javascript, I can transform a given element via a SVG.Matrix
like so:
import * as SVG from 'svg.js';
let draw = SVG('drawing').size(200, 200);
let rect = draw.rect(100, 100).attr({ fill: '#f06' });
rect.translate(100,100);
let matrix = new SVG.Matrix(rect);
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(matrix);
In the example the 2nd rect gets transformed like the first rect via SVG.Matrix
:
<svg id="SvgjsSvg1001" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="100" height="100" fill="#ff0066" transform="matrix(1,0,0,1,100,100)"></rect><rect id="SvgjsRect1009" width="100" height="100" fill="#ff6600" transform="matrix(1,0,0,1,100,100)"></rect></svg>
However the same code in typescript claims:
[ts]
Argument of type 'Matrix' is not assignable to parameter of type 'Transform'.
Types of property 'skewX' are incompatible.
Type '(x: number, cx?: number, cy?: number) => Matrix' is not assignable to type 'number'.
How can I transform an element via a SVG.Matrix
in typescript?
Why does svgjs.Element.transform
behave different when used with ts?
<svg id="SvgjsSvg1001" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="100" height="100" fill="#ff0066" transform="matrix(1,0,0,1,100,100)"></rect><rect id="SvgjsRect1009" width="100" height="100" fill="#ff6600" transform="matrix(1,0,0,1,100,100)"></rect></svg>
<svg id="SvgjsSvg1001" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="100" height="100" fill="#ff0066" transform="matrix(1,0,0,1,100,100)"></rect><rect id="SvgjsRect1009" width="100" height="100" fill="#ff6600" transform="matrix(1,0,0,1,100,100)"></rect></svg>
typescript svg matrix svg.js
typescript svg matrix svg.js
edited Nov 25 '18 at 14:03
kai-dj
asked Nov 25 '18 at 13:24
kai-djkai-dj
1599
1599
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
svg.js.d.ts
tells that Element.transform
has this signature:
interface Element {
transform(t: Transform, relative?: boolean): Element;
transform(): Transform;
...
}
So, this should work:
const transform = rect.transform();
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(transform);
Essentially, both Transform
and Matrix
objects have a/b/c/d/e/f properties, so in Javascript one can be used in place of the other, but in Typescript, they have no prototypical relation and the compiler complains.
It's actually a bug in the typings. Looking at the source code, the transformation is implemented by calling new Matrix(t)
, so the signature to .transform()
should read:
transform(t: MatrixAlias, relative?: boolean): Element;
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
1
See edit: is hacking the.d.ts
for now an option?
– ccprog
Nov 25 '18 at 15:38
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467925%2fhow-to-use-svg-jss-transform-with-matrix-parameter-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
svg.js.d.ts
tells that Element.transform
has this signature:
interface Element {
transform(t: Transform, relative?: boolean): Element;
transform(): Transform;
...
}
So, this should work:
const transform = rect.transform();
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(transform);
Essentially, both Transform
and Matrix
objects have a/b/c/d/e/f properties, so in Javascript one can be used in place of the other, but in Typescript, they have no prototypical relation and the compiler complains.
It's actually a bug in the typings. Looking at the source code, the transformation is implemented by calling new Matrix(t)
, so the signature to .transform()
should read:
transform(t: MatrixAlias, relative?: boolean): Element;
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
1
See edit: is hacking the.d.ts
for now an option?
– ccprog
Nov 25 '18 at 15:38
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
add a comment |
svg.js.d.ts
tells that Element.transform
has this signature:
interface Element {
transform(t: Transform, relative?: boolean): Element;
transform(): Transform;
...
}
So, this should work:
const transform = rect.transform();
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(transform);
Essentially, both Transform
and Matrix
objects have a/b/c/d/e/f properties, so in Javascript one can be used in place of the other, but in Typescript, they have no prototypical relation and the compiler complains.
It's actually a bug in the typings. Looking at the source code, the transformation is implemented by calling new Matrix(t)
, so the signature to .transform()
should read:
transform(t: MatrixAlias, relative?: boolean): Element;
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
1
See edit: is hacking the.d.ts
for now an option?
– ccprog
Nov 25 '18 at 15:38
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
add a comment |
svg.js.d.ts
tells that Element.transform
has this signature:
interface Element {
transform(t: Transform, relative?: boolean): Element;
transform(): Transform;
...
}
So, this should work:
const transform = rect.transform();
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(transform);
Essentially, both Transform
and Matrix
objects have a/b/c/d/e/f properties, so in Javascript one can be used in place of the other, but in Typescript, they have no prototypical relation and the compiler complains.
It's actually a bug in the typings. Looking at the source code, the transformation is implemented by calling new Matrix(t)
, so the signature to .transform()
should read:
transform(t: MatrixAlias, relative?: boolean): Element;
svg.js.d.ts
tells that Element.transform
has this signature:
interface Element {
transform(t: Transform, relative?: boolean): Element;
transform(): Transform;
...
}
So, this should work:
const transform = rect.transform();
let rect2 = draw.rect(100, 100).attr({ fill: '#f60' });
rect2.transform(transform);
Essentially, both Transform
and Matrix
objects have a/b/c/d/e/f properties, so in Javascript one can be used in place of the other, but in Typescript, they have no prototypical relation and the compiler complains.
It's actually a bug in the typings. Looking at the source code, the transformation is implemented by calling new Matrix(t)
, so the signature to .transform()
should read:
transform(t: MatrixAlias, relative?: boolean): Element;
edited Nov 25 '18 at 15:37
answered Nov 25 '18 at 14:42
ccprogccprog
9,29521127
9,29521127
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
1
See edit: is hacking the.d.ts
for now an option?
– ccprog
Nov 25 '18 at 15:38
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
add a comment |
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
1
See edit: is hacking the.d.ts
for now an option?
– ccprog
Nov 25 '18 at 15:38
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
Thank you for answering. It does work for the example I gave, but my real usecase would be to apply multiplied matrices to elements. SVG.Transform has a matrix-method – but I'm still left with the problem of applying the matrix… SVG.Transform does not offer a multiply-method as far as I can tell.
– kai-dj
Nov 25 '18 at 14:53
1
1
See edit: is hacking the
.d.ts
for now an option?– ccprog
Nov 25 '18 at 15:38
See edit: is hacking the
.d.ts
for now an option?– ccprog
Nov 25 '18 at 15:38
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Thanks for reply. I think yes, it would be. Maybe even a good opportunity for me to gain further ts knowledge :)
– kai-dj
Nov 25 '18 at 16:07
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
Works :) I'll file an issue and pull request on github.com/svgdotjs/svg.js (?)
– kai-dj
Nov 25 '18 at 16:22
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
@ccprog We are aware that our typings are not comlete. However, we are lacking the knowledge and time to complete / correct them. Feel free, to open a PR with fixes. Every bit helps!
– Fuzzyma
Nov 29 '18 at 8:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467925%2fhow-to-use-svg-jss-transform-with-matrix-parameter-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown