简体   繁体   English

如何使用JavaScript更改图像?

[英]How to alter images with JavaScript?

I wanted to build a JS applet for a user to customize his product color before ordering. 我想为用户构建一个JS小程序,以便在订购前自定义产品颜色。

An example can be seen here - http://www.lunacommerce.com/diy-ipone4-parts.php 可以在此处看到一个示例-http: //www.lunacommerce.com/diy-ipone4-parts.php

I was unable to find the JS code in the page. 我无法在页面中找到JS代码。 Can anyone guide on how to go about to build this applet? 谁能指导如何建立这个小程序? I have a basic level knowledge of JS but I'm ready to learn whatever is required for building this. 我具有JS的基础知识,但是我准备学习构建此JS所需的任何知识。

JavaScript cannot be used to modify client-side images directly (except, maybe some canvas -powered solutions for filters). JavaScript不能直接用于修改客户端图像(也许有些canvas支持的过滤器解决方案除外)。

Hint: the image on that site could be semi-transparent with a colored backlayer. 提示:该位置上的图像可能是半透明的,并带有彩色的背层。

选择的是不同的图像,而不是更改了颜色的图像。

This is done by putting together the phone by many small images as parts. 这是通过将许多小图像作为一部分将手机放在一起来完成的。 For example those are 2 home buttons: 例如,这些是2个主页按钮:

http://www.lunacommerce.com/modules/blocklunatopics/media/base/1300875928.png http://www.lunacommerce.com/modules/blocklunatopics/media/base/1300875928.png

http://www.lunacommerce.com/modules/blocklunatopics/media/base/1300875939.png http://www.lunacommerce.com/modules/blocklunatopics/media/base/1300875939.png

With JavaScript they just change the images. 使用JavaScript,他们只需更改图像即可。

First of all, nothing is 'alter' in this 'applet'. 首先,在这个“小程序”中没有什么“改变”的。

The client side will do an Ajax call each time you select a new color and this ajax call will return an image url from the choosen color. 每次您选择新颜色时,客户端都会进行一次Ajax调用,并且此ajax调用将返回所选颜色的图像url。

You need to know: What is an Ajax call and how it works with the server side. 您需要了解:什么是Ajax调用及其在服务器端的工作方式。

You can check: 您可以检查:

http://www.w3schools.com/ajax/default.asp http://www.w3schools.com/ajax/default.asp

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

You better read: 您最好阅读:

http://en.wikipedia.org/wiki/JavaScript http://en.wikipedia.org/wiki/JavaScript

If the question is how to modify an image with client side JS, the answer is using canvas, https://developer.mozilla.org/en/Canvas_tutorial/Using_images . 如果问题是如何使用客户端JS修改图像,则答案是使用canvas https://developer.mozilla.org/en/Canvas_tutorial/Using_images You can render an image in it and modify it to your liking, then you can use 您可以在其中渲染图像并根据自己的喜好对其进行修改,然后可以使用

var strDataURI = canvas.toDataURL(); 
// returns "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACt..."

You can then send that to the server or open a new page with it 然后,您可以将其发送到服务器或使用它打开一个新页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM