You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
/*
|
|
* Copyright (C) 2019 Intel Corporation
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
// Translate point array from the client coordinate system
|
|
// to a coordinate system of a canvas
|
|
export function translateFromSVG(svg: SVGSVGElement, points: number[]): number[] {
|
|
const output = [];
|
|
const transformationMatrix = svg.getScreenCTM();
|
|
let pt = svg.createSVGPoint();
|
|
for (let i = 0; i < points.length - 1; i += 2) {
|
|
pt.x = points[i];
|
|
pt.y = points[i + 1];
|
|
pt = pt.matrixTransform(transformationMatrix);
|
|
output.push(pt.x, pt.y);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
// Translate point array from a coordinate system of a canvas
|
|
// to the client coordinate system
|
|
export function translateToSVG(svg: SVGSVGElement, points: number[]): number[] {
|
|
const output = [];
|
|
const transformationMatrix = svg.getScreenCTM().inverse();
|
|
let pt = svg.createSVGPoint();
|
|
for (let i = 0; i < points.length; i += 2) {
|
|
pt.x = points[i];
|
|
pt.y = points[i + 1];
|
|
pt = pt.matrixTransform(transformationMatrix);
|
|
output.push(pt.x, pt.y);
|
|
}
|
|
|
|
return output;
|
|
}
|