- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让 Canvas 上的图像有可能在其上有一个低不透明度的框,以显示它已被选中。所以我需要一个 onclick 函数,但我不知道该怎么做。有什么建议吗?如果有人能指出正确的方向,让我知道如何让盒子也跳动,我也将不胜感激。
var image1 = new Kinetic.Image({
image: imageObj,
x: xposition,
y: yposition,
width: width,
height: height,
stroke: 'blue',
srokeFill: 'red',
strokeWidth: 5,
draggable: true,
dragBoundFunc: function (pos) {
if (pos.x < this.minX)
this.minX = pos.x;
return {
x: pos.x,
y: this.getAbsolutePosition().y
}
}
});
layer.on('mouseover', function (evt) {
var shape = evt.target;
document.body.style.cursor = 'pointer';
shape.strokeEnabled(true);
layer.draw();
});
layer.on('mouseout', function (evt) {
var shape = evt.target;
document.body.style.cursor = 'default';
shape.strokeEnabled(false);
layer.draw();
});
最佳答案
在 Canvas 上制作可点击区域时,您需要这样做。
- Create a redraw function.
- Find the mouse point inside of canvas.
- Do collision detection on the mouse point and region.
更新:添加了多个可点击区域。
// x1, y1, x2, y2, clicked
var boxes = [{
x1: 10,
y1: 10,
x2: 110,
y2: 110,
clicked: false
}, {
x1: 120,
y1: 10,
x2: 220,
y2: 110,
clicked: false
}];
function go() {
var can = document.getElementById('can');
var ctx = can.getContext('2d');
var w = can.width;
var h = can.height;
makeNoise(ctx);
var imageData = ctx.getImageData(0, 0, w, h);
var mx = 0;
var my = 0;
$('#can').mousemove(function(event) {
var offset = $(this).offset();
mx = event.pageX - offset.left;
my = event.pageY - offset.top;
redraw();
});
$('#can').click(function(event) {
var offset = $(this).offset();
mx = event.pageX - offset.left;
my = event.pageY - offset.top;
for (var i = 0, len = boxes.length; i < len; i++) {
var b = boxes[i];
if (hit(b.x1, b.y1, b.x2, b.y2)) {
b.clicked = !b.clicked;
}
}
redraw();
});
function redraw() {
// Draw the original image
ctx.putImageData(imageData, 0, 0);
// Draw the clickable region
for (var i = 0, len = boxes.length; i < len; i++) {
drawBox(boxes[i]);
}
// Draw the mouse. Probably only needed when testing
// mouse location.
drawMouse();
}
redraw();
// Collision dection of a square against mouse point.
// square points x1,y1 must be less than x2,y2
function hit(x1, y1, x2, y2) {
if (mx < x1 || my < y1 || mx > x2 || my > y2) return false;
return true;
}
function drawMouse() {
ctx.moveTo(mx, my);
ctx.fillStyle = "yellow";
ctx.beginPath();
ctx.arc(mx, my, 5, 0, Math.PI * 360);
ctx.fill();
}
function drawBox(b) {
if (b.clicked) {
// Mouse clicked.
ctx.fillStyle = "rgba(255,0,255,.5)"
} else if (hit(b.x1, b.y1, b.x2, b.y2)) {
// Mouse Over
ctx.fillStyle = "rgba(255,255,255,.5)"
} else {
// Mouse Out
ctx.fillStyle = "red";
}
ctx.fillRect(b.x1, b.y1, b.x2 - b.x1, b.y2 - b.y1);
}
}
// Fill a canvas context with noise.
function makeNoise(ctx) {
var can = ctx.canvas;
var imageData = ctx.getImageData(0, 0, can.width, can.height);
var d = imageData.data;
for (var i = 0, len = d.length; i < len; i += 4) {
d[i] = d[i + 1] = d[i + 2] = (Math.random() * 8) << 4;
d[i + 3] = 255;
}
ctx.putImageData(imageData, 0, 0);
}
go();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="can" width="400" height="300"></canvas>
关于单击时的 JavaScript 笔划填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31257630/
所以我试图通过使用套接字创建一个类似同步绘画程序的东西。我有一个服务器端..和一个客户端,我试图将inkCollection从服务器发送到客户端。这适用于简单的文本,但我似乎无法发送inkCollec
我正在胡思乱想,无法控制路径宽度。 看到倒数第二行,真是莫名其妙: var paper = Raphael(20, 20, 320, 320); function arc(center, radi
我想提一下,这篇文章类似于 Continuous drawing in CGContext with drawRect但它没有任何代码片段解决方案,所以再次询问。 我正在学习 iOS 开发应用程序。我
我正在创建很多元素,这些元素基本上基于模板,但具有不同的大小和颜色 - 圆圈、方框、星号、菱形、图表中经常使用的元素。我发现我可以使用 SVG Symbol 来定义我的模板,如下例所示: http:/
我正在使用 Chart.js 创建圆环图,但遇到了圆环上的笔划被 Canvas 对象 chop 的问题。 Canvas 上的填充/边距并没有为我解决问题。知道发生了什么事吗? JSFiddle her
我试过设置描边颜色。它可以使用 linearGradient 但不能使用 solidColor :
我目前正在调整文件输入以使用预览显示其内容 标签。 ..... 使用一些 CSS,它在 Chrome 上正确呈现,但我在 Firefox 上(并且仅在 Firefox 上)在正
我有一组三 Angular 坐标。每三个点一个 vector 。在下图中,这个三 Angular 形是 thin yellow 三 Angular 形。我需要向这个向内和向外突出的三 Angular
我是一名优秀的程序员,十分优秀!