|
| 1 | +import { animated, useSpring } from 'react-spring'; |
| 2 | +import React, { useEffect, useRef, useState } from 'react'; |
| 3 | +import { useAgile } from '@agile-ts/react'; |
| 4 | +import core from '../../../../../../core'; |
| 5 | +import styles from './styles.module.css'; |
| 6 | +import clsx from 'clsx'; |
| 7 | + |
| 8 | +type Props = { className?: string }; |
| 9 | + |
| 10 | +const Astronaut: React.FC<Props> = (props) => { |
| 11 | + const { className } = props; |
| 12 | + const [timing] = useState(200); |
| 13 | + const [isRaised, setIsRaised] = React.useState(false); |
| 14 | + const animated_Astronaut = useSpring({ |
| 15 | + transform: isRaised ? `translateY(-${30}px)` : `translateY(0px)`, |
| 16 | + config: { |
| 17 | + mass: 1, |
| 18 | + tension: 400, |
| 19 | + friction: 15, |
| 20 | + }, |
| 21 | + }); |
| 22 | + const dark = useAgile(core.ui.ASTRONAUT_DARK); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (!isRaised) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const timeoutId = setTimeout(() => { |
| 30 | + core.ui.toggleAstronautColor(!dark); |
| 31 | + setIsRaised(false); |
| 32 | + }, timing); |
| 33 | + |
| 34 | + return () => clearTimeout(timeoutId); |
| 35 | + }, [isRaised, timing]); |
| 36 | + |
| 37 | + const imageRef = useRef<HTMLImageElement>(null); |
| 38 | + const ctx = document.createElement('canvas').getContext('2d'); |
| 39 | + |
| 40 | + // https://medium.com/@pdx.lucasm/canvas-with-react-js-32e133c05258 |
| 41 | + // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent |
| 42 | + function trigger(event: React.MouseEvent<HTMLImageElement, MouseEvent>) { |
| 43 | + console.log('on click'); |
| 44 | + |
| 45 | + console.log(event); |
| 46 | + console.log(ctx); |
| 47 | + |
| 48 | + // Get click coordinates |
| 49 | + const x = event.pageX - imageRef.current.offsetLeft; |
| 50 | + const y = event.pageY - imageRef.current.offsetTop; |
| 51 | + const w = (ctx.canvas.width = imageRef.current.width); |
| 52 | + const h = (ctx.canvas.height = imageRef.current.height); |
| 53 | + |
| 54 | + // Draw image to canvas |
| 55 | + // and read Alpha channel value |
| 56 | + ctx.drawImage(imageRef.current, 0, 0, w, h); |
| 57 | + const alpha = ctx.getImageData(x, y, 1, 1).data[3]; // [0]R [1]G [2]B [3]A |
| 58 | + |
| 59 | + console.log(ctx.getImageData(x, y, 1, 1)); |
| 60 | + |
| 61 | + // If pixel is transparent, |
| 62 | + // retrieve the element underneath and trigger it's click event |
| 63 | + if (alpha === 0) { |
| 64 | + console.log('alpha0'); |
| 65 | + imageRef.current.style.pointerEvents = 'none'; |
| 66 | + const element = document.elementFromPoint(event.clientX, event.clientY); |
| 67 | + // element.trigger('click'); |
| 68 | + imageRef.current.style.pointerEvents = 'auto'; |
| 69 | + } else { |
| 70 | + setIsRaised(true); |
| 71 | + console.log('LOGO clicked!'); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className={clsx(styles.Container, className)}> |
| 77 | + <animated.img |
| 78 | + ref={imageRef} |
| 79 | + onMouseEnter={trigger} |
| 80 | + style={animated_Astronaut} |
| 81 | + className={styles.Image} |
| 82 | + src={`img/astronaut-${dark ? 'dark' : 'light'}.svg`} |
| 83 | + alt={'Astronaut'} |
| 84 | + /> |
| 85 | + <div className={styles.Text}>Poke me 👆 to mutate my color State.</div> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default Astronaut; |
0 commit comments