html5-canvas에서 이미지 내부의 이미지 색상을 변경하고 싶습니다.

프라 빈

캔버스를 사용하여 배지를 만들고 있습니다. 이미지를 그리기 위해 코드를 사용했습니다.

let image = new Image()
image.src = 'imageSource'
image.onload = () => {
 ctx.drawImage(image, xOffset, yOffset, newWidth, newHeight)

 // to color the image at the back (works properly)

 ctx.globalCompositeOperation = 'source-in'
 ctx.fillStyle = 'someColour'
 ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)
 ctx.globalCompositeOperation = 'source-over'

  // icon in the middle

  let iconImage = new Image()
  iconImage.src = 'iconSource'
  iconImage.onload = () => {
      ctx.drawImage(iconImage, xOffset, yOffset, width, height)

      // i need to be able to fill color in this iconImage only
}
  

미리보기는 이렇습니다.

캔버스 이미지

이제 이미지에 색상을 지정하기 위해 다양한 블렌드 모드를 사용해 보았습니다. 인 배경에 대해 작업하면 잘 작동합니다 image. iconImage같은 방식으로 시도했지만 작동하지 않았습니다. 다른 것을 변경하지 않고 가운데에있는 아이콘에 색상을 지정하고 싶습니다.

b3hr4d

저는 아침에 지루해서이 예제를 만들었습니다.이 예제에서는 캔버스의 모든 요소를 ​​수정할 수 있음을 알 수 있습니다.

참고 : CORS 문제 ( 오염 된 캔버스는 내보낼 수 없음)로 인해 여기에서 외부 이미지의 색상을 편집 할 수 없으므로 파일 선택을 사용하여 이미지를 가져온 다음 이미지 색상을 변경하십시오!

const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d")
const inputs = document.querySelectorAll("input");
const xOffset = 30, yOffset = 10, width = canvas.width-60, height = canvas.height-20;

var inputValues = {stroke:"#8db5c2",fill:"white",text:"Text",image:"https://i.stack.imgur.com/8eLMW.png",imageColor:"grey"}

inputs.forEach(input => {
  input.addEventListener("input", function() {
    if(this.id === "image") {
      if (!input.files || !input.files[0]) return;
      const FR = new FileReader();
      FR.onloadend = (evt) => {
          inputValues = {...inputValues,[this.id]:FR.result};
          DrawBadge(inputValues)
      };
      FR.readAsDataURL(input.files[0]);
    } else {
      inputValues = {...inputValues,[this.id]:this.value};
      DrawBadge(inputValues)
    }
  })
})

DrawBadge(inputValues)

function DrawBadge ({stroke, fill, text, image ,imageColor}) {
  //Draw Badge
  ctx.strokeStyle = stroke;
  ctx.lineWidth = 15;
  ctx.fillStyle = fill;
  roundRect(ctx, xOffset, yOffset, width, height, {
    tl: 1,
    tr: 1,
    bl: width/2,
    br: width/2,
  });
  //Draw Text
  ctx.font = "20px Arial";
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillStyle = "black";
  ctx.fillText(text,width/2+xOffset,height*0.8);
  //Draw Image
  const firstImage = new Image();
  const insideWidth = 80, insideHeight = 80;
  firstImage.src = image;
  // Because of the CORS issue just show image as it is
  if(image === "https://i.stack.imgur.com/8eLMW.png") {
    firstImage.onload = () => {
      ctx.drawImage(firstImage, (width/2)-(insideWidth/2)+xOffset,height*0.2,insideWidth , insideHeight);
     }
  // you should use this function for changing image color
  } else {
    firstImage.onload = () => {
      //Make new canvas for image
      const imageCtx = document.createElement("canvas").getContext("2d");
      const insideImage = new Image();
      imageCtx.canvas.width = insideWidth;
      imageCtx.canvas.height = insideHeight;
      imageCtx.save();
      imageCtx.fillStyle = imageColor;
      imageCtx.fillRect(0, 0, insideWidth, insideHeight);
      //Here magic happend 
      imageCtx.globalCompositeOperation = "destination-in";
      imageCtx.drawImage(firstImage,0,0,insideWidth,insideHeight);
      //Then export our canvas to png image
      insideImage.src = imageCtx.canvas.toDataURL("image/png");
      insideImage.onload = () => {
          ctx.drawImage(insideImage,(width/2)-(insideWidth/2)+xOffset,height*0.2,insideWidth,insideHeight);
      }
    }
  }
}

function roundRect(ctx, x, y, width, height, radius, fill, stroke){
  ctx.beginPath();
  ctx.moveTo(x + radius.tl, y);
  ctx.lineTo(x + width - radius.tr, y);
  ctx.quadraticCurveTo(x + width, y, x + width, y + radius.tr);
  ctx.lineTo(x + width, y + height - radius.br);
  ctx.quadraticCurveTo(x + width, y + height, x + width - radius.br, y + height);
  ctx.lineTo(x + radius.bl, y + height);
  ctx.quadraticCurveTo(x, y + height, x, y + height - radius.bl);
  ctx.lineTo(x, y + radius.tl);
  ctx.quadraticCurveTo(x, y, x + radius.tl, y);
  ctx.closePath();
  ctx.fill();
  ctx.stroke();
}
body {
 display: flex;
}
#inputs {
  display: flex;
  flex-direction: column;
}
canvas {
   border: 1px solid;
}
<body>
  <div id="inputs">
    Stroke Color: <input id="stroke" type="color" value="#8db5c2">
    Fill Color: <input id="fill" type="color" value="#ffffff">
    Text: <input id="text" type="text" value="Text">
    <lable>
    Image:<input id="image" type="file"accept="image/png, image/jpeg">
     ImageColor: <input id="imageColor" type="color" value="#808080">
     </lable>
  </div>
  <canvas width="220" height="190"></canvas>
</body>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

HTML5 Canvas 이미지 변색

분류에서Dev

HTML5 Canvas : 이미지 색상 변경

분류에서Dev

HTML5 Canvas의 초 고해상도 이미지

분류에서Dev

HTML5 캔버스 .toDataURL () 이미지에 배경색이 없습니다.

분류에서Dev

마우스 오버시 이미지의 배경색을 변경하고 싶습니다.

분류에서Dev

이미지보기의 이미지 색상을 변경할 수 없습니다.

분류에서Dev

Julia의 이미지에 논리적 마스킹을하고 싶습니다.

분류에서Dev

html에서 inputstream에 이미지를 저장 했으므로 inputstream을 내 드라이브의 폴더에 저장하고 싶습니다.

분류에서Dev

파이 게임의 이미지에 원을 그리고 싶습니다

분류에서Dev

kivy의 그리드 레이아웃에서 위젯 색상을 동적으로 변경하고 싶습니다.

분류에서Dev

Phonegap의 HTML5 Canvas에 이미지를 그릴 수 없습니다.

분류에서Dev

laravel의 뷰에 이미지를 표시하고 싶습니다.

분류에서Dev

버튼을 클릭하여 UIButton 및 UIImage보기를 변경하고 싶습니다. 하지만 UIButton과 이미지보기의 이미지는 다릅니다.

분류에서Dev

IE 8에서는 이미지 맵을 사용한 HTML5 이미지 회전이 작동하지 않습니다. 이유가 무엇입니까?

분류에서Dev

mouseover html5 canvas에서 사용자 정의 커서 이미지가 변경되지 않음

분류에서Dev

내 PHP 페이지에서 이미지 ID로 이미지를 열고 싶습니다.

분류에서Dev

HTML5 캔버스는 모양이 오버레이되는 이미지에 색상을 적용합니다.

분류에서Dev

HTML5 Canvas에서 이미지를 호출하는 방법

분류에서Dev

xml에서 버튼의 사용자 정의 모양을 정의했습니다. 이제 색상을 동적으로 변경하고 싶습니다. 어떻게?

분류에서Dev

pyplot은 이미지의 색상을 변경합니다.

분류에서Dev

클릭 기능은 이미지의 색상을 반전하고 이전에 반전 된 이미지의 색상을 복원해야합니다.

분류에서Dev

SQL에서 여러 이미지를 검색하고 지정된 위치의 jsp 페이지에 인쇄하고 싶습니다.

분류에서Dev

SwiftUI-이미지 아이콘의 색상을 변경할 수 없습니다.

분류에서Dev

내가 링크 한 이미지와 같이 내 시야에가는 선을 그리고 싶습니다

분류에서Dev

html에 버튼을 만들고 싶지만 이미지에 맞게 조정할 수 없습니다.

분류에서Dev

5 개의 이미지가 있고이를 단일 ImageButton의 src로 설정하고 싶습니다. 클릭 할 때마다 변경되어야하며 앱이 닫혀도 값을 유지해야합니다.

분류에서Dev

업로드 된 이미지를 html 이미지 태그에 표시하고 싶습니다.

분류에서Dev

Unity에서 버튼의 텍스트 및 이미지 색상을 변경하는 방법은 무엇입니까?

분류에서Dev

Python에서 이미지의 색상 채널을 변경하는 방법은 무엇입니까?

Related 관련 기사

  1. 1

    HTML5 Canvas 이미지 변색

  2. 2

    HTML5 Canvas : 이미지 색상 변경

  3. 3

    HTML5 Canvas의 초 고해상도 이미지

  4. 4

    HTML5 캔버스 .toDataURL () 이미지에 배경색이 없습니다.

  5. 5

    마우스 오버시 이미지의 배경색을 변경하고 싶습니다.

  6. 6

    이미지보기의 이미지 색상을 변경할 수 없습니다.

  7. 7

    Julia의 이미지에 논리적 마스킹을하고 싶습니다.

  8. 8

    html에서 inputstream에 이미지를 저장 했으므로 inputstream을 내 드라이브의 폴더에 저장하고 싶습니다.

  9. 9

    파이 게임의 이미지에 원을 그리고 싶습니다

  10. 10

    kivy의 그리드 레이아웃에서 위젯 색상을 동적으로 변경하고 싶습니다.

  11. 11

    Phonegap의 HTML5 Canvas에 이미지를 그릴 수 없습니다.

  12. 12

    laravel의 뷰에 이미지를 표시하고 싶습니다.

  13. 13

    버튼을 클릭하여 UIButton 및 UIImage보기를 변경하고 싶습니다. 하지만 UIButton과 이미지보기의 이미지는 다릅니다.

  14. 14

    IE 8에서는 이미지 맵을 사용한 HTML5 이미지 회전이 작동하지 않습니다. 이유가 무엇입니까?

  15. 15

    mouseover html5 canvas에서 사용자 정의 커서 이미지가 변경되지 않음

  16. 16

    내 PHP 페이지에서 이미지 ID로 이미지를 열고 싶습니다.

  17. 17

    HTML5 캔버스는 모양이 오버레이되는 이미지에 색상을 적용합니다.

  18. 18

    HTML5 Canvas에서 이미지를 호출하는 방법

  19. 19

    xml에서 버튼의 사용자 정의 모양을 정의했습니다. 이제 색상을 동적으로 변경하고 싶습니다. 어떻게?

  20. 20

    pyplot은 이미지의 색상을 변경합니다.

  21. 21

    클릭 기능은 이미지의 색상을 반전하고 이전에 반전 된 이미지의 색상을 복원해야합니다.

  22. 22

    SQL에서 여러 이미지를 검색하고 지정된 위치의 jsp 페이지에 인쇄하고 싶습니다.

  23. 23

    SwiftUI-이미지 아이콘의 색상을 변경할 수 없습니다.

  24. 24

    내가 링크 한 이미지와 같이 내 시야에가는 선을 그리고 싶습니다

  25. 25

    html에 버튼을 만들고 싶지만 이미지에 맞게 조정할 수 없습니다.

  26. 26

    5 개의 이미지가 있고이를 단일 ImageButton의 src로 설정하고 싶습니다. 클릭 할 때마다 변경되어야하며 앱이 닫혀도 값을 유지해야합니다.

  27. 27

    업로드 된 이미지를 html 이미지 태그에 표시하고 싶습니다.

  28. 28

    Unity에서 버튼의 텍스트 및 이미지 색상을 변경하는 방법은 무엇입니까?

  29. 29

    Python에서 이미지의 색상 채널을 변경하는 방법은 무엇입니까?

뜨겁다태그

보관