|
| 1 | +import { ReactSketchCanvas } from "react-sketch-canvas"; |
| 2 | +import { type ChangeEvent, useState } from "react"; |
| 3 | + |
| 4 | +const somePreserveAspectRatio = [ |
| 5 | + "none", |
| 6 | + "xMinYMin", |
| 7 | + "xMidYMin", |
| 8 | + "xMaxYMin", |
| 9 | + "xMinYMid", |
| 10 | + "xMidYMid", |
| 11 | + "xMaxYMid", |
| 12 | + "xMinYMax", |
| 13 | + "xMidYMax", |
| 14 | + "xMaxYMax", |
| 15 | +] as const; |
| 16 | + |
| 17 | +type SomePreserveAspectRatio = (typeof somePreserveAspectRatio)[number]; |
| 18 | + |
| 19 | +export default function App() { |
| 20 | + const [backgroundImage, setBackgroundImage] = useState( |
| 21 | + "https://images.pexels.com/photos/1193743/pexels-photo-1193743.jpeg?cs=srgb&fm=jpg", |
| 22 | + ); |
| 23 | + const [preserveAspectRatio, setPreserveAspectRatio] = |
| 24 | + useState<SomePreserveAspectRatio>("none"); |
| 25 | + |
| 26 | + const handlePreserveAspectRatioChange = ( |
| 27 | + event: ChangeEvent<HTMLSelectElement>, |
| 28 | + ) => { |
| 29 | + setPreserveAspectRatio(event.target.value as SomePreserveAspectRatio); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleBackgroundImageChange = ( |
| 33 | + event: ChangeEvent<HTMLInputElement>, |
| 34 | + ) => { |
| 35 | + setBackgroundImage(event.target.value); |
| 36 | + }; |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className="d-flex flex-column gap-2 p-2"> |
| 40 | + <h1>Tools</h1> |
| 41 | + <div className="d-flex gap-2 flex-column"> |
| 42 | + <div className="mb-3"> |
| 43 | + <label htmlFor="backgroundImage" className="form-label"> |
| 44 | + Background Image |
| 45 | + </label> |
| 46 | + <input |
| 47 | + type="text" |
| 48 | + className="form-control" |
| 49 | + id="backgroundImage" |
| 50 | + placeholder="URL of the image to use as a background" |
| 51 | + value={backgroundImage} |
| 52 | + onChange={handleBackgroundImageChange} |
| 53 | + /> |
| 54 | + </div> |
| 55 | + <label htmlFor="preserveAspectRatio" className="form-label"> |
| 56 | + Preserve Aspect Ratio |
| 57 | + </label> |
| 58 | + <select |
| 59 | + id="preserveAspectRatio" |
| 60 | + className="form-select form-select-sm" |
| 61 | + aria-label="Preserve Aspect Ratio options" |
| 62 | + value={preserveAspectRatio} |
| 63 | + onChange={handlePreserveAspectRatioChange} |
| 64 | + > |
| 65 | + {somePreserveAspectRatio.map((value) => ( |
| 66 | + <option key={value} value={value}> |
| 67 | + {value} |
| 68 | + </option> |
| 69 | + ))} |
| 70 | + </select> |
| 71 | + </div> |
| 72 | + <h1>Canvas</h1> |
| 73 | + <ReactSketchCanvas |
| 74 | + backgroundImage={backgroundImage} |
| 75 | + preserveBackgroundImageAspectRatio={preserveAspectRatio} |
| 76 | + /> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +} |
0 commit comments