- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我的代码只能拖入矩形或正方形,但我如何逐渐将 Angular 拖入正方形?
这是我的代码链接:https://codepen.io/firassyazwani/pen/NWWKMGK
case 'topleft':
rect.w += rect.x - mousePos.x;
rect.h += rect.y - mousePos.y;
rect.x = mousePos.x;
rect.y = mousePos.y;
break;
case 'topright':
rect.w = mousePos.x - rect.x;
rect.h += rect.y - mousePos.y;
rect.y = mousePos.y;
break;
case 'bottomleft':
rect.w += rect.x - mousePos.x;
rect.x = mousePos.x;
rect.h = mousePos.y - rect.y;
break;
case 'bottomright':
rect.w = mousePos.x - rect.x;
rect.h = mousePos.y - rect.y;
break;
case 'top':
rect.h += rect.y - mousePos.y;
rect.y = mousePos.y;
break;
case 'left':
rect.w += rect.x - mousePos.x;
rect.x = mousePos.x;
break;
case 'bottom':
rect.h = mousePos.y - rect.y;
break;
case 'right':
rect.w = mousePos.x - rect.x;
break;
}
最佳答案
您可以使用 isSquare
bool 值通过修改 point()
和 mousemove
事件的开关大小写来使高度和宽度均匀变化:
var isSquare = true;
...
function point(x, y, isSquare) {
return !isSquare ? {
x: x,
y: y
} : {
x: x,
y: x
};
}
...
case 'topright':
rect.w = mousePos.x - rect.x;
if (!isSquare) {
rect.y = mousePos.y;
rect.h += rect.y - mousePos.y;
} else {
rect.h = mousePos.x - rect.x;
rect.y = rect.x;
}
break;
...
case 'bottomleft':
rect.w += rect.x - mousePos.x;
rect.x = mousePos.x;
if (!isSquare) {
rect.h = mousePos.y - rect.y;
} else {
rect.h = rect.w;
rect.y = rect.x;
}
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
rect = {
x: 50,
y: 50,
w: 50,
h: 50
},
handlesSize = 8,
currentHandle = false,
drag = false,
prevW = rect.w,
prevH = rect.h,
isSquare = true;
function init() {
canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mouseup', mouseUp, false);
canvas.addEventListener('mousemove', mouseMove, false);
}
function point(x, y, isSquare) {
return !isSquare ? {
x: x,
y: y
} : {
x: x,
y: x
};
}
function dist(p1, p2) {
return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
}
function getHandle(mouse) {
if (dist(mouse, point(rect.x, rect.y)) <= handlesSize)
return 'topleft';
if (dist(mouse, point(rect.x + rect.w, rect.y)) <= handlesSize)
return 'topright';
if (dist(mouse, point(rect.x, rect.y + rect.h)) <= handlesSize)
return 'bottomleft';
if (dist(mouse, point(rect.x + rect.w, rect.y + rect.h)) <= handlesSize)
return 'bottomright';
if (dist(mouse, point(rect.x + rect.w / 2, rect.y)) <= handlesSize)
return 'top';
if (dist(mouse, point(rect.x, rect.y + rect.h / 2)) <= handlesSize)
return 'left';
if (dist(mouse, point(rect.x + rect.w / 2, rect.y + rect.h)) <= handlesSize)
return 'bottom';
if (dist(mouse, point(rect.x + rect.w, rect.y + rect.h / 2)) <= handlesSize)
return 'right';
return false;
}
function mouseDown(e) {
if (currentHandle)
drag = true;
draw();
}
function mouseUp() {
drag = false;
currentHandle = false;
prevW = rect.w;
prevH = rect.h;
draw();
}
function mouseMove(e) {
var previousHandle = currentHandle;
if (!drag)
currentHandle = getHandle(point(e.pageX - this.offsetLeft, e.pageY - this.offsetTop));
if (currentHandle && drag) {
var mousePos = point(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
switch (currentHandle) {
case 'topleft':
rect.w += rect.x - mousePos.x;
rect.h += rect.y - mousePos.y;
rect.x = mousePos.x;
rect.y = mousePos.y;
break;
case 'topright':
rect.w = mousePos.x - rect.x;
if (!isSquare) {
rect.y = mousePos.y;
rect.h += rect.y - mousePos.y;
} else {
rect.h = mousePos.x - rect.x;
rect.y = rect.x;
}
break;
case 'bottomleft':
rect.w += rect.x - mousePos.x;
rect.x = mousePos.x;
if (!isSquare) {
rect.h = mousePos.y - rect.y;
} else {
rect.h = rect.w;
rect.y = rect.x;
}
break;
case 'bottomright':
rect.w = mousePos.x - rect.x;
rect.h = mousePos.y - rect.y;
break;
case 'top':
rect.h += rect.y - mousePos.y;
rect.y = mousePos.y;
break;
case 'left':
rect.w += rect.x - mousePos.x;
rect.x = mousePos.x;
break;
case 'bottom':
rect.h = mousePos.y - rect.y;
break;
case 'right':
rect.w = mousePos.x - rect.x;
break;
}
//console.log(mousePos, rect, currentHandle);
}
if (drag || currentHandle != previousHandle)
draw();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#FFFF33';
ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
if (currentHandle) {
var posHandle = point(0, 0);
switch (currentHandle) {
case 'topleft':
posHandle.x = rect.x;
posHandle.y = rect.y;
break;
case 'topright':
posHandle.x = rect.x + rect.w;
posHandle.y = rect.y;
break;
case 'bottomleft':
posHandle.x = rect.x;
posHandle.y = rect.y + rect.h;
break;
case 'bottomright':
posHandle.x = rect.x + rect.w;
posHandle.y = rect.y + rect.h;
break;
case 'top':
posHandle.x = rect.x + rect.w / 2;
posHandle.y = rect.y;
break;
case 'left':
posHandle.x = rect.x;
posHandle.y = rect.y + rect.h / 2;
break;
case 'bottom':
posHandle.x = rect.x + rect.w / 2;
posHandle.y = rect.y + rect.h;
break;
case 'right':
posHandle.x = rect.x + rect.w;
posHandle.y = rect.y + rect.h / 2;
break;
}
ctx.globalCompositeOperation = 'xor';
ctx.beginPath();
ctx.arc(posHandle.x, posHandle.y, handlesSize, 0, 2 * Math.PI);
ctx.fill();
ctx.globalCompositeOperation = 'source-over';
}
if (drag) {
if (rect.w != prevW) {
ctx.fillText(rect.w,
rect.x + rect.w / 2 - 5,
rect.y - 10);
}
if (rect.h != prevH) {
ctx.fillText(rect.h,
rect.x + rect.w + 10,
rect.y + rect.h / 2 + 5);
}
}
}
init();
draw();
body {
background-color: #a3d5d3;
}
#canvas {
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
<canvas id="canvas" width="500" height="500"></canvas>
关于javascript - 如何逐渐拖入正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58296182/
我想要一个可以逐渐将页面底部的不透明度从 0 更改为 1 的函数。 我在元素的顶部使用了类似的功能,但我使用的不是淡入,而是淡出。创建它相当容易,但在页面底部使用设置的阈值淡入是一场噩梦。 经过研究,
li 状态是关闭和打开。 闭合:颜色为黑色(没有光照,相互折叠)。 打开:颜色为红色(有光,全视野)。 我希望颜色从两个元素之间的点开始淡化,以表明阴影从轴心开始,直到它们关闭。 $('button'
我的问题很简单: 1)我有一个扩展 JFrame 的主类。 2)该类内部有一个 JPanel,其顶部有一个 BufferedImage。 3)最后还有一个 JButton,我称之为“Fire”..这就
我有这个立方体,我想在延迟 3000 后将其转换为不同的 X 和 Y 点。我无法理解如何在 jQuery 的帮助下做到这一点。这是一个JS Fiddle .下面是代码。 JS // code for
我们如何向背景图像添加黑色阴影,阴影从不透明度 1 开始逐渐降低到不透明度 0,在图像的所有 4 个边上? (至少 50 像素值的“降低阴影不透明度”。box-shadow 仅提供少量不透明度逐渐下降
我是一名优秀的程序员,十分优秀!