我们的json文件中有两种图像。...
1. 蒙版图像
2. 背景图片:
Json的蒙版图片示例:
在JSON中,遮罩图像名称将以“ mask _ ”
开头 {
"src" : "image1.png",
"name" : "mask_image_1"
},
背景图像的示例Json:
{
"src" : "image2.png",
"name" : "bg_image"
},
现在两者图像都是可点击,这意味着一旦单击图像,我们将显示文件上传对话框 ...
要求:
只有蒙版图像应该是可点击的,而不是背景图像。...
因此我为container添加了此条件,但是此后,两个图像都变得不可点击。...
if (name.indexOf('mask_')! == -1)
{
$('.container').click(function(e)
{
// container code here
});
}
Codepen:https://codepen.io/kidsdial/pen/GeVbea
提琴:https://jsfiddle.net/kidsdial1/s9xfo4zj/7/
下面是代码段:
var target;
var imageUrl = "https://i.imgur.com/RzEm1WK.png";
let jsonData = {
"layers" : [
{
"x" : 0,
"height" : 1012,
"layers" : [
{
"x" : 365,
"layers" : [
{
"x" : 0,
"src" : "ax0HVTs.png",
"y" : 0,
"name" : "mask_image_1"
}
],
"y" : 21,
"name" : "user_image_1"
},
{
"x" : 25,
"layers" : [
{
"x" : 0,
"src" : "iEA642D.jpg",
"y" : 0,
"name" : "bg_image"
}
],
"y" :22,
"name" : "L2"
}
],
"y" : 0,
"width" : 1012,
"name" : "L1"
}
]
};
$(document).ready(function() {
// below code will upload image onclick mask image
$('.container').click(function(e) {
var res = e.target;
target = res.id;
console.log(target);
if (e.target.getContext)
{
setTimeout(() => {
$('#fileup').click();
}, 20);
}
});
// Below code will fetch mask images from json file
function getAllSrc(layers) {
let arr = [];
layers.forEach(layer => {
if (layer.src) {
arr.push({
src: layer.src,
x: layer.x,
y: layer.y,
name: layer.name
});
} else if (layer.layers) {
let newArr = getAllSrc(layer.layers);
if (newArr.length > 0) {
newArr.forEach(({
src,
x,
y,
name
}) => {
arr.push({
src,
x: (layer.x + x),
y: (layer.y + y),
name: (name)
});
});
}
}
});
return arr;
}
function json(data)
{
let arr = getAllSrc(data.layers);
let layer1 = data.layers;
let counter = 0;
let table = [];
for (let {
src,
x,
y,
name
} of arr) {
var mask = $(".container").mask({
imageUrl: imageUrl,
maskImageUrl: 'https://i.imgur.com/' + src,
onMaskImageCreate: function(img) {
img.css({
"position": "absolute",
"left": x + "px",
"top": y + "px"
});
},
id: counter
});
table.push(mask);
fileup.onchange = function() {
let mask2 = table[target];
mask2.loadImage(URL.createObjectURL(fileup.files[0]));
document.getElementById('fileup').value = "";
};
counter++;
}
}
json(jsonData);
}); // end of document ready
// jq plugin
(function($) {
var JQmasks = [];
$.fn.mask = function(options) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
maskImageUrl: undefined,
imageUrl: undefined,
scale: 1,
id: new Date().getUTCMilliseconds().toString(),
x: 0, // image start position
y: 0, // image start position
onMaskImageCreate: function(div) {},
}, options);
var container = $(this);
let prevX = 0,
prevY = 0,
draggable = false,
img,
canvas,
context,
image,
timeout,
initImage = false,
startX = settings.x,
startY = settings.y,
div;
container.updateStyle = function() {
return new Promise((resolve, reject) => {
context.beginPath();
context.globalCompositeOperation = "source-over";
image = new Image();
image.setAttribute('crossOrigin', 'anonymous');
image.src = settings.maskImageUrl;
image.onload = function()
{
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0, image.width, image.height);
div.css({
"width": image.width,
"height": image.height
});
resolve();
};
});
};
function renderInnerImage() {
img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.src = settings.imageUrl;
img.onload = function() {
settings.x = settings.x == 0 && initImage ? (canvas.width - (img.width * settings.scale)) / 2 : settings.x;
settings.y = settings.y == 0 && initImage ? (canvas.height - (img.height * settings.scale)) / 2 : settings.y;
context.globalCompositeOperation = 'source-atop';
context.drawImage(img, settings.x, settings.y, img.width * settings.scale, img.height * settings.scale);
initImage = false;
};
}
// change the draggable image
container.loadImage = function(imageUrl) {
console.log("load");
//if (img)
// img.remove();
// reset the code.
settings.y = startY;
settings.x = startX;
prevX = prevY = 0;
settings.imageUrl = imageUrl;
initImage = true;
container.updateStyle().then(renderInnerImage);
};
// change the masked Image
container.loadMaskImage = function(imageUrl, from) {
canvas = document.createElement("canvas");
context = canvas.getContext('2d');
canvas.setAttribute("draggable", "true");
canvas.setAttribute("id", settings.id);
settings.maskImageUrl = imageUrl;
div = $("<div/>", {
"class": "masked-img"
}).append(canvas);
// div.find("canvas").on('touchstart mousedown', function(event)
container.append(div);
if (settings.onMaskImageCreate)
settings.onMaskImageCreate(div);
container.loadImage(settings.imageUrl);
};
container.loadMaskImage(settings.maskImageUrl);
JQmasks.push({
item: container,
id: settings.id
})
return container;
};
}(jQuery));
.temp {}
.container {
background: gold;
position: relative;
width:812px;
height:812px;
}
.container img {
position:absolute;
top:0;
bottom:250px;
left:0;
right:0;
margin:auto;
z-index:999;
}
.masked-img {
overflow: hidden;
position: relative;
}
.txtContainer{ position:absolute; text-align:center; color:#FFF}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<input id="fileup" name="fileup" type="file" style="display:none" >
<div class="container">
</div>
答案 0 :(得分:2)
使用JS只需添加pointer-events: none
,以便不可点击
if (name.indexOf('mask_') !== -1) {
var imageUrl1 = imageUrl;
} else {
var imageUrl1 = '';
$('.masked-img').css('pointer-events', 'none');
}