You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Util.convertFreehandPointsToBase64 = (points, annotation) => {
// Define a margin to add padding around the path.
const margin = 0;
// Calculate the bounding box of the points.
const minX = Math.min(...points.map(p => p[0]));
const minY = Math.min(...points.map(p => p[1]));
const maxX = Math.max(...points.map(p => p[0]));
const maxY = Math.max(...points.map(p => p[1]));
// Adjust the width and height to include the margin.
const adjustedWidth = maxX - minX + margin * 2;
const adjustedHeight = maxY - minY + margin * 2;
// Adjust the points to start from (margin, margin).
const adjustedPoints = points.map(([x, y]) => [x - minX + margin, y - minY + margin]);
// Generate the path data using the adjusted points.
const pathData = Util.getSvgPathFromStroke(adjustedPoints);
// Create the SVG with the adjusted path.
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="${adjustedWidth}" height="${adjustedHeight}">
<path d="${pathData}" fill="${annotation.color}" />
</svg>
`.trim();
// Convert the SVG to a Base64-encoded data URL.
return 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svg)));
};
// From the lib
Util.getSvgPathFromStroke = (points, closed = false) => {
const average = (a, b) => (a + b) / 2;
const len = points.length
if (len < 4) {
return '';
}
let a = points[0]
let b = points[1]
const c = points[2]
let result = `M${a[0].toFixed(2)},${a[1].toFixed(2)} Q${b[0].toFixed(
2
)},${b[1].toFixed(2)} ${average(b[0], c[0]).toFixed(2)},${average(
b[1],
c[1]
).toFixed(2)} T`;
for (let i = 2, max = len - 1; i < max; i++) {
a = points[i]
b = points[i + 1]
result += `${average(a[0], b[0]).toFixed(2)},${average(a[1], b[1]).toFixed(
2
)} `;
}
if (closed) {
result += 'Z';
}
return result;
};
I am somehow confused that the renderin shrinks once saved to svg. Cannot figure out why. Any idea? Even if i set the adjustedWidth and adjustedHeight to the annotation.w and annotation.h (which will contain the svg base64) the rendering changes a little bit and is not the same as when i drew it in canvas.
I included an image of the 2. The first is when i drew it in canvas. The second is when i exported it to svg using the functions above.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi i am using these functions
I am somehow confused that the renderin shrinks once saved to svg. Cannot figure out why. Any idea? Even if i set the adjustedWidth and adjustedHeight to the annotation.w and annotation.h (which will contain the svg base64) the rendering changes a little bit and is not the same as when i drew it in canvas.
I included an image of the 2. The first is when i drew it in canvas. The second is when i exported it to svg using the functions above.
Thoughts?

Beta Was this translation helpful? Give feedback.
All reactions