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
406 views
in Technique[技术] by (71.8m points)

javascript - Hide transparent image from download while using React Konva

I want to download only the papayawhip square box without the transparent image behind it.

dont download transparent image

I am using React Konva. I have the following code:

import * as React from "react"
import { Stage, Layer, Rect } from "react-konva"
import type { Stage as StageType } from "konva/types/Stage"
import { observer } from "mobx-react"

import "./styles.css"
import { useStore } from "./context"

const downloadURI = (uri: string | undefined, name: string | undefined) => {
    const link = document.createElement("a")
    link.download = name as string
    link.href = uri as string
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}

function App() {
    const [fillColor, setFillColor] = React.useState("")
    const [downloadClicked, setDownloadClicked] = React.useState(false)
    const stageRef = React.useRef<StageType>(null)
    const transparentBackground = new window.Image()
    transparentBackground.src =
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAMAAAC6V+0/AAAABlBMVEUAAADY2NjnFMi2AAAAAXRSTlMAQObYZgAAABVJREFUGNNjYIQDBgQY0oLDxBsIQQCltADJNa/7sQAAAABJRU5ErkJggg=="

    const store = useStore()
    const { win, canvas, browser, pad } = store

    return (
        <div className="flex">
            <div id="sidebar">
                <h1 className="text">
                    Center <span>papayawhip</span> Canvas
                </h1>
                <input
                    type="text"
                    placeholder="Enter Fill Color"
                    value={fillColor}
                    onChange={(e) => setFillColor(e.target.value)}
                />
                <button
                    className="inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-indigo-600 border border-transparent rounded-md shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                    onClick={() => {
                        setDownloadClicked(true)
                        const options = { mimeType: `image/png`, quality: 1, pixelRatio: 1 }
                        const img = stageRef.current?.getStage().toDataURL(options)
                        downloadURI(img, "download.png")
                        setDownloadClicked(false)
                    }}
                >
                    Download Image
                </button>
            </div>
            <Stage
                ref={stageRef}
                width={canvas.width}
                height={canvas.height}
                id="konva"
            >
                <Layer>
                    {fillColor === "" && downloadClicked && (
                        <Rect
                            width={browser.width + 200}
                            height={browser.height + 200}
                            x={pad / 2}
                            y={(win.height - browser.height) / 2}
                            fillPatternImage={transparentBackground}
                            fill={fillColor}
                        />
                    )}
                    <Rect
                        width={browser.width}
                        height={browser.height}
                        x={pad / 2}
                        y={(win.height - browser.height) / 2}
                        fill="papayawhip"
                    />
                </Layer>
            </Stage>
        </div>
    )
}

export default observer(App)

Codesandbox → https://codesandbox.io/s/add-padding-to-centered-canvas-with-sidebar-gqhhl?file=/src/App.tsx

How do I download only the papayawhip rectangle part?

question from:https://stackoverflow.com/questions/65858087/hide-transparent-image-from-download-while-using-react-konva

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

1 Reply

0 votes
by (71.8m points)

In principle,

  1. hide any shapes you don't want included in the image,
  2. Get the image
  3. unhide what you hid in 1.

Note: do not draw the layer/stage between 1 and 3 or you will see an unwanted flicker.


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

...