这是我的第一个 html5 项目,了解它的要点,需要一些帮助。我使用 jquery UI 滑块向画布发送不透明度值,它工作得非常好......但是当滑块处于关闭位置且值为 0 时,我无法让图像完全关闭。这是我正在尝试做的事情:http: //jsfiddle.net/N6wZZ/2/
这是我的 JS:
$("#slider").slider({
animate: true,
range: "min",
value: 0,
min: 0,
max: 0.9,
step: .01,
create: function (event, ui) {
var opacityValue = '0.0';
canvasFunction(opacityValue);
},
slide: function (event, ui) {
$('#hiddenField').attr('value', ui.value);
$("#slider-result").html(ui.value);
var opacityValue = $('#hiddenField').val();
if (opacityValue == 0) {
var opacityValue = 0;
var workAroundVar = 300;
var workAroundVarTwo = 0;
canvasFunction(opacityValue, workAroundVar, workAroundVarTwo);
}
else {
var workAroundVar = 0;
var workAroundVarTwo = 300;
canvasFunction(opacityValue, workAroundVar, workAroundVarTwo);
}
}
});
function canvasFunction(opacityValue, workAroundVar, workAroundVarTwo){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var img1 = loadImage('http://moosepic.com/test2.png', main);
var img2 = loadImage('http://moosepic.com/test.png', main);
var imagesLoaded = 0;
function main() {
imagesLoaded += 1;
if (imagesLoaded == 2) {
// composite now
ctx.drawImage(img1, 0, 0, 300, 300);
ctx.globalAlpha = opacityValue;
ctx.drawImage(img2, workAroundVar, workAroundVar, workAroundVarTwo, workAroundVarTwo);
}
}
function loadImage(src, onload) {
var img = new Image();
img.onload = onload;
img.src = src;
return img;
}
}
如您所见,我做了很多变通方法来关闭第二张图像或将其从画布中剔除。任何帮助都是极好的。