Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

fabricjs - Fabric.js group.toDataURL() is exporting a pixelated image

So I'm using Fabric.js to load in a bunch of images onto a canvas side by side. A user can then select one of the images to download. They also have edit capabilities so can add text and shapes ontop of the image using canvas.

When a user exports and image they expect to see any text or shapes they have added to be included in the export.

right now I have the following function to pass in the selected image and grab its area on the canvas to use in the toDataURL() function. This results in a super blurry image i guess because in snapshots the area wholelistically.

Anyone know a way that i could select the objects in the area of the selecte dobjects for export?

const onDownload = ({ activeObject, projectName, canvas }) => {
    console.log(activeObject);

    const activeObjs = canvas.getObjects();
    const group = new fabric.Group(activeObjs);
    canvas.add(group);

    const left = Math.abs(group.aCoords.tl.x - activeObject.aCoords.tl.x);
    const top = Math.abs(group.aCoords.tl.y - activeObject.aCoords.tl.y);

    const url = group.toDataURL({
        type: 'png',
        top: top,
        left: left,
        width: activeObject.width,
        height: activeObject.height,
    });

    group.destroy();
    canvas.remove(group);
    canvas.renderAll();

    var link = document.createElement('a');
    link.href = url;
    link.download = `${projectName}.png`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
};
question from:https://stackoverflow.com/questions/66048685/fabric-js-group-todataurl-is-exporting-a-pixelated-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...