Crop icon image so background is equal to the perimeter of icon

Viewed 32

fire icons that I want to crop

How can I crop this image so the background only extends to the edge of the icon? I would like to color each icon like the image I provided, but I need to remove the square background and have only the space inside the icon change color, can someone help me with this?

Here is the jsx for the icon component

import React from 'react';
import fireIconImage from '../images/fireIcon.png';

export default function FireIcon(props) {
    return (
        <img
            onClick={props.onClick}
            onMouseLeave={props.onMouseLeave}
            onMouseOver={props.onMouseOver}
            className={props.className}
            src={fireIconImage}
            alt="fire icon"
            id={props.id}
        ></img>
    )
}

Here is the jsx for the parent component that holds the icons

import React from 'react'
import fireIconImage from '../images/fireIcon.png'
import FireIcon from './FireIcon'

export default function BlogPostForm () {
    const [formState, setFormState] = React.useState({ flaire: '', title: '', text: ''});
    const [isHovered, setIsHovered] = React.useState();
    const [isLit, setIsLit] = React.useState();

    function changeFlaire(event) {
        const selectedFlaire = event.target.value;
        setFormState( {...formState, flaire: selectedFlaire });
    }

    function changeTitle(event) {
        const title = event.target.value;
        setFormState( {...formState, title: title });
    }

    function changeText(event) {
        const text = event.target.value;
        setFormState( {...formState, text: text });
    }

    function handleMouseOver(e) {
        setIsHovered(e.target.id);
    }

    function handleMouseLeave(e) {
        setIsHovered();
    }

    function handleFireIconClick(e) {
        setIsLit(e.target.id);
    }

    function handleFireIconClass(fireLevel) {
        const classNames = ['fireIcon']
            classNames.push(`fireIcon${fireLevel}`)
            if (isHovered >= fireLevel) {
                classNames.push('isHeld')
            }
            if (isLit >= fireLevel) {
                classNames.push('isLit')
            }
            console.log(classNames)
        return classNames.join(' ');
        
    }


    const fireIconsArray = [];

    for (let i = 0; i < 5; i++) {
        fireIconsArray.push(
            <FireIcon 
                onClick={handleFireIconClick}
                onMouseLeave={handleMouseLeave}
                onMouseOver={handleMouseOver}
                className={handleFireIconClass(i+1)}
                src={fireIconImage}
                alt="fire icon"
                id={i+1}
            />
        )
    }

    console.log(fireIconsArray);
    
    return (
        <form className="postForm">
            <h1 className="postFormHeader">Create a blog post!</h1>        
                <select
                    required
                    className="flaireSelect" 
                    value={formState.flaire}
                    onChange={changeFlaire}>
                        <option disabled={true} value="">Choose a flaire</option>
                        <option value="JavaScript">JavaScript</option>
                        <option value="CSS">CSS</option>
                        <option value="HTML">HTML</option>
                        <option value="REACT">REACT</option>
                        <option value="BACKEND">BACKEND</option>
                </select>
                <input
                    value={formState.title}
                    onChange={changeTitle}
                    className="titleBox"
                    placeholder="title"
                    type="text"
                    id="title"
                    name="title"
                />
                <textarea
                    value={formState.text}
                    onChange={changeText}
                    className="textBox"
                    placeholder="text"
                    type="text"
                    id="blogPost"
                    name="blogPost"
                />
                <div className="fireIconContainer">
                    {fireIconsArray}
                </div>  
                <div className="blogPostFormButtonContainer">
                    <button className="blogPostSubmit" type="submit">SUBMIT</button>
                    <button className="blogPostCancel" type="submit">CANCEL</button>
                </div>
        </form>
    )
}

Here is the css

/*blogPostForm */
.postForm {
  font-family: "Montserrat", sans-serif;
  color: white;
  background-color: var(--color2);
  border-radius: 10px;
  margin-top: 10px;
  width: 80%;
  max-width: 750px;

  display: grid;
  align-items: center;
  justify-content: center;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  grid-template-areas:
    "flaire"
    "title"
    "post"
    "buttons";
  grid-gap: 5px;
}

.postFormHeader {
  justify-self: center;
}

.flaireSelect {
  font-family: "Montserrat", sans-serif;
  justify-self: center;
  width: 150px;
  height: 35px;
  text-align: center;
}

select:invalid {
  color: grey;
}

.titleBox {
  font-family: "Montserrat", sans-serif;
  height: 40px;
  margin: 10px;
}

.textBox {
  font-family: "Montserrat", sans-serif;
  height: 100px;
  margin: 10px;
  resize: vertical;
}

.fireIconContainer {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.fireIcon {
  color: white;
  width: 40px;
  height: 40px;
  transition: all 0.2s ease-in-out;
}

.fireIcon1.isLit {
  background-color: #ffd000;
}

.fireIcon2.isLit {
  background-color: rgb(255, 136, 0);
}

.fireIcon3.isLit {
  background-color: hsl(22, 100%, 50%);
}

.fireIcon4.isLit {
  background-color: rgb(255, 51, 0);
}

.fireIcon5.isLit {
  background-color: rgb(187, 26, 4);
}

.isHeld {
  transform: scale(1.1);
}

.blogPostFormButtonContainer {
  font-family: "Montserrat", sans-serif;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin-bottom: 10px;
}

.blogPostSubmit {
  font-family: "Montserrat", sans-serif;
  width: 35%;
  margin: 5px;
  height: 35px;
}

.blogPostCancel {
  font-family: "Montserrat", sans-serif;
  width: 35%;
  margin: 5px;
  height: 35px;
}
1 Answers

I would recommend doing something like that with svg & not png. I made a CSS animation for demo purpose.

here I am changing colors of the fire every one second with fill property on the svg. i hope this is not confusing...

body {
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}

svg {
  width: 100px;
}

.st0 {
  fill-rule: evenodd;
  clip-rule: evenodd;
  fill: #ffd100;
  stroke: black;
  stroke-width: 2px;
  animation: firestate 5s infinite;
}

.st1 {
  fill-rule: evenodd;
  clip-rule: evenodd;
  fill: #ffd100;
  stroke: black;
  stroke-width: 2px;
  animation: firestate 5s infinite;
}

@keyframes firestate {
  25% {
    fill: #ff8800;
  }
  50% {
    fill: #ff5e00;
  }
  75% {
    fill: #ff3200;
  }
  100% {
    fill: #bb1a04;
  }
}
<body>
  <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 92.27 122.88" style="enable-background:new 0 0 92.27 122.88" xml:space="preserve">
        <g>
            <path class="st0"
                d="M18.61,54.89C15.7,28.8,30.94,10.45,59.52,0C42.02,22.71,74.44,47.31,76.23,70.89 c4.19-7.15,6.57-16.69,7.04-29.45c21.43,33.62,3.66,88.57-43.5,80.67c-4.33-0.72-8.5-2.09-12.3-4.13C10.27,108.8,0,88.79,0,69.68 C0,57.5,5.21,46.63,11.95,37.99C12.85,46.45,14.77,52.76,18.61,54.89L18.61,54.89z" />
            <path class="st1"
                d="M33.87,92.58c-4.86-12.55-4.19-32.82,9.42-39.93c0.1,23.3,23.05,26.27,18.8,51.14 c3.92-4.44,5.9-11.54,6.25-17.15c6.22,14.24,1.34,25.63-7.53,31.43c-26.97,17.64-50.19-18.12-34.75-37.72 C26.53,84.73,31.89,91.49,33.87,92.58L33.87,92.58z" />
        </g>
    </svg>
</body>

Related