HTML5 画布滑块
HTML5 Canvas slider
尝试创建一个滑块类,让您轻松快速地自定义值。您可以拖动手柄或单击滑块的任何部分以将手柄移动到那里,它可以工作......有点。您可以根据需要单击任意次数,但是...如果你甚至点击一次或拖动,然后松开并重试,它只会下降(永远不会上升)一点点。这听起来可能令人困惑,所以这里是小提琴。
var Slider = createEntity({
init: function (args) {
args = args || {};
this.x = args.x || 0;
this.y = args.y || 0;
this.width = args.width || 10;
this.height = args.height || 100;
this.min = args.min || 0;
this.max = args.max || 100;
this.value = args.value || 50;
this.rotation = args.rotation || 0;
this.on = args.on || function () {};
var backFill = randHsla();
args.back = args.back || {};
this.back = args.back;
args.back.fill = args.back.fill || backFill;
this.back.fill = args.back.fill;
args.back.borderFill = args.back.borderFill || backFill;
this.back.borderFill = args.back.borderFill;
args.back.width = args.back.width || 5;
this.back.width = args.back.width;
args.back.x = args.back.x || this.width / 2 - this.back.width / 2;
this.back.x = args.back.x;
var handleColor = randHsla();
args.handle = args.handle || {};
this.handle = args.handle;
args.handle.fill = args.handle.fill || handleColor;
this.handle.fill = args.handle.fill;
args.handle.borderStroke = args.handle.borderStroke || handleColor;
this.handle.borderStroke = args.handle.borderStroke;
args.handle.height = args.handle.height || 5;
this.handle.height = args.handle.height;
args.handle.y = args.handle.y || 0;
this.handle.y = args.handle.y;
this.updatePos();
},
draw: function (fx) {
fx.save();
fx.translate(this.x, this.y);
fx.rotate(this.rotation);
fx.fillStyle = this.back.fill;
fx.beginPath();
fx.fillRect(this.back.x, 0, this.back.width, this.height);
fx.closePath();
fx.fillStyle = this.handle.fill;
fx.strokeStyle = this.handle.borderStroke;
fx.beginPath();
fx.rect(0, this.handle.y, this.width, this.handle.height);
fx.closePath();
fx.fill();
fx.stroke();
fx.restore();
},
updateVal: function () {
var oldVal = this.value,
handleRange = this.height - this.handle.height,
valRange = this.max - this.min;
this.value = (handleRange - this.handle.y) / handleRange * valRange + this.min;
if (this.on instanceof Function && this.value !== oldVal) {
this.on();
}
return this;
},
updatePos: function () {
var handleRange = this.height - this.handle.height,
valRange = this.max - this.min;
this.handle.y = handleRange - ((this.value - this.min) / valRange) * handleRange;
return this;
},
getMouse: function (map) {
var self = this,
mouse = getMouse(map),
bounds = {};
setBounds();
map.addEventListener('mousedown', function (event) {
if (hasPoint(bounds, mouse.x, mouse.y)) {
map.addEventListener('mousemove', onMouseMove);
map.addEventListener('mouseup', onMouseUp);
} else if (hasPoint(self, mouse.x, mouse.y)) {
var y = mouse.y - self.y;
self.handle.y = Math.min(self.height - self.handle.height, Math.max(y, 0));
self.updateVal();
}
});
function onMouseUp(event) {
map.removeEventListener('mousemove', onMouseMove, false);
map.removeEventListener('mouseup', onMouseUp, false);
}
function onMouseMove(event) {
var y = mouse.y - self.y;
self.handle.y = Math.min(self.height - self.handle.height, Math.max(y, 0));
self.updateVal();
}
function setBounds() {
bounds.x = self.x;
bounds.y = self.y + self.handle.y;
bounds.width = self.width;
bounds.height = self.handle.height;
}
return this;
}
});
可以在此处找到createEntity
和hasPoint
等外部函数。
单击滑块并在第一个滑块后松开后,我将如何使其工作?
如果用户还单击控点,则添加事件侦听器以启用拖动。
map.addEventListener('mousedown', function(event) {
if (hasPoint(bounds, mouse.x, mouse.y)) {
map.addEventListener('mousemove', onMouseMove);
map.addEventListener('mouseup', onMouseUp);
} else if (hasPoint(self, mouse.x, mouse.y)) {
map.addEventListener('mousemove', onMouseMove);
map.addEventListener('mouseup', onMouseUp);
var y = mouse.y - self.y;
self.handle.y = Math.min(self.height - self.handle.height, Math.max(y, 0));
self.updateVal();
}
});
相关文章:
- 是否可以为HTML5输入滑块触发oninput事件
- HTML5画布动画滑块
- HTML5范围滑块最小值和最大值
- 在滑块中控制 HTML5 视频
- CSS3 HTML5 滑块滑动
- javascript用于带有html5的自动图像滑块
- HTML5范围滑块值在使用jQuery(而不是UI)的过程中
- Html5输入滑块未更新
- 带有页面导航问题的 HTML5 滑块
- HTML5 画布滑块
- 如何仅在移动设备上创建HTML5滑块,否则jQuery UI
- 使用 JQuery 从 HTML5 滑块检索值
- 如何获取 HTML5 范围(2 张幻灯片)滑块在 PHP 中的值
- 如何使用键盘箭头键移动 html5 范围滑块手柄
- 如何在PHP中获取HTML5范围滑块的值
- HTML5 音频元素 - 查找滑块 - 无法在“HTMLMediaElement”上设置“当前时间”属性:提供的双精度值
- 什么'这个HTML5,Javascript滑块代码有问题
- jQuery HTML5范围滑块中的实时输出
- 切换视频源-滑块上的同一时间-JS,HTML5视频
- Html5-canvas javascript绘制线与滑块