且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实时替换图像上的颜色

更新时间:2023-02-19 15:02:00

be:





主要是在所有的浏览器像素操作与画布可能非常慢也与小图像。因此,另一个实验可以是获取像素颜色,然后:




  • 将颜色信息传递到PHP(或另一个服务器端脚本)用AJAX调用

  • 使用 GD imagemagik

  • 返回带有Ajax响应的图片

  • 使用修改后的图片版本重新载入您的画布


First of all I will explain my situation so you can know my problem a little better. I'm making a HTML5 app. I have a canvas, and using a color picker you can change the color of the canvas. Now i have a picture which I want to put on the canvas but that pictures color needs to be changed using a color picker. So i need to replace, lets say, black color on that picture and put it on the canvas so it dosnt screw up the background.

So that will look like this:
1st color picker- changes the color of the canvas
2nd color picker - replaces the black color on the image with the one in the color picker and puts it on the canvas

Now my problem is how to replace the color on the image without reloading the page.

My only condition is no using silverlight, flash, java or any other similar tehnology that need 3rd party software to be installed on the device.

Thanks in advance. If you dont understand my query fully, feel free to ask.

My approach with a JS only solution could be:

After some canvas experiment I notice that mostly in all the browser the pixel manipulation with canvas could be very slow also with small images. So another experiment to do could be to get the pixel color and then:

  • pass the color information to a PHP (or another server side script) with an AJAX call
  • do the color manipulation with an image library like GD or imagemagik
  • return back your image with the Ajax response
  • reload your canvas with the modified version of the image