Calculate degrees of Linear Gradient in Canvas using Konva with React?
Asked Answered
W

1

3

I want to calculate the degree used in a Linear Gradient β†’ linear-gradient(140deg, rgba(165, 142, 251, 1), rgb(233, 191, 248)) into x & y co-ordinates to use it in Konva, which is basically a wrapper around Canvas.

I have found quite similar questions with a caveat that they are answered in vanilla Canvas, not Konva like:

But when I tried implementing them, I don't get the same desired effect as I get in CSS (see the comparison):

linear-gradient comparison in konva vs css

The code is quite similar to what is posted in some of the answers above:

import { Stage, Layer, Rect } from "react-konva"

// linear-gradient(140deg, rgba(165, 142, 251, 1), rgb(233, 191, 248))
export default function App() {
    const width = window.innerWidth / 1.25 // random width
    const height = window.innerHeight / 1.5 // random height

    const x1 = 0
    const y1 = 0
    const angle = (140 / 180) * Math.PI
    const length = width
    const x2 = x1 + Math.cos(angle) * length
    const y2 = y1 + Math.sin(angle) * length

    return (
        <div className="App">
            <h1>Linear Gradient in Konva πŸ‘‡</h1>
            <Stage width={width} height={height}>
                <Layer>
                    <Rect
                        name="transparentBackground"
                        width={width}
                        height={height}
                        x={0}
                        y={0}
                        fillPriority="linear-gradient" // 'color', 'pattern', 'linear-gradient', 'radial-gradient'
                        /* linear-gradient */
                        fillLinearGradientStartPoint={{ x: x1, y: y1 }}
                        fillLinearGradientEndPoint={{ x: x2, y: y2 }}
                        fillLinearGradientColorStops={[
                            0,
                            "rgba(165, 142, 251, 1)",
                            1,
                            "rgb(233, 191, 248)",
                        ]}
                    />
                </Layer>
            </Stage>

            <h1>CSS Gradient πŸ‘‡</h1>
            <div
                style={{
                    marginTop: 10,
                    width,
                    height,
                    backgroundImage:
                        "linear-gradient(140deg, rgba(165, 142, 251, 1), rgb(233, 191, 248))",
                }}
            ></div>
        </div>
    )
}

I think the error is in length as I don't know what it should be it's certainly not clear. Also, not sure about the x1 & y1 co-ordinates as I think they should be zero & hence, can be removed.

How do I get the same effect?

Codesandbox β†’ https://codesandbox.io/s/linear-gradient-in-react-konva-cpgrk?file=/src/App.tsx

Woodie answered 1/5, 2021 at 13:16 Comment(0)
W
4

Found the answer on a subreddit for Game Developers /r/gamedev where I shouldn't have asked but I did & it worked.

import { Stage, Layer, Rect } from "react-konva"

// linear-gradient(140deg, rgba(165, 142, 251, 1), rgb(233, 191, 248))
export default function App() {
    const width = window.innerWidth / 1.25 // random width
    const height = window.innerHeight / 1.5 // random height

    // Specify angle in degrees
    const angleInDeg = 140

    // Compute angle in radians - CSS starts from 180 degrees and goes clockwise
    // Math functions start from 0 and go anti-clockwise so we use 180 - angleInDeg to convert between the two
    const angle = ((180 - angleInDeg) / 180) * Math.PI

    // This computes the length such that the start/stop points will be at the corners
    const length =
        Math.abs(width * Math.sin(angle)) + Math.abs(height * Math.cos(angle))

    // Compute the actual x,y points based on the angle, length of the gradient line and the center of the div
    const halfx = (Math.sin(angle) * length) / 2.0
    const halfy = (Math.cos(angle) * length) / 2.0
    const cx = width / 2.0
    const cy = height / 2.0
    const x1 = cx - halfx
    const y1 = cy - halfy
    const x2 = cx + halfx
    const y2 = cy + halfy

    return (
        <div className="App">
            <h1>Linear Gradient in Konva πŸ‘‡</h1>
            <Stage width={width} height={height}>
                <Layer>
                    <Rect
                        name="transparentBackground"
                        width={width}
                        height={height}
                        x={0}
                        y={0}
                        fillPriority="linear-gradient" // 'color', 'pattern', 'linear-gradient', 'radial-gradient'
                        /* linear-gradient */
                        fillLinearGradientStartPoint={{ x: x1, y: y1 }}
                        fillLinearGradientEndPoint={{ x: x2, y: y2 }}
                        fillLinearGradientColorStops={[
                            0,
                            "rgba(165, 142, 251, 1)",
                            1,
                            "rgb(233, 191, 248)",
                        ]}
                    />
                </Layer>
            </Stage>

            <h1>CSS Gradient πŸ‘‡</h1>
            <div
                style={{
                    marginTop: 10,
                    width,
                    height,
                    backgroundImage: `linear-gradient(${angleInDeg}deg, rgba(165, 142, 251, 1), rgb(233, 191, 248))`,
                }}
            ></div>
        </div>
    )
}

Codesandbox β†’ https://codesandbox.io/s/linear-gradient-in-react-konva-cpgrk?file=/src/App.tsx

Woodie answered 2/5, 2021 at 13:3 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.