动态改变SVG光标 [英] Dynamically change SVG cursor
问题描述
我已经创建了 SVG 光标,我想在鼠标滚动时动态更改光标半径,放大增加半径,缩小减小半径.
I have created SVG cursor and I would like to dynamically change cursor radius on mouse scroll, zoom in increase radius, zoom out decrease radius.
CSS SVG 光标,这是有效的:
CSS SVG cursor, this works:
.brushCursor {
cursor: url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r="5" stroke-width="2" style="stroke: black; fill: red;"/></svg>') 10 10, pointer;
}
但我想动态更改 SVG 的半径.半径应该在 1 到 10 的范围内.到目前为止,我做了这个,但光标没有更新:
But I want to change SVG's radius dynamically. Radius should be in the range 1 to 10. So far I made this but the cursor is not updating:
const brush = document.querySelector('.brush');
const radius = 5;
brush.style.cursor = `url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r=${radius} stroke-width="2" style="stroke: black; fill: red;"/></svg>')`;
推荐答案
我正在创建一个新的 元素
s
并且我正在添加规则到那个.
I'm creating a new <style>
element s
and I'm adding the rule to that.
input type='range'
用于更改半径.
let r = 3;
let s = document.createElement("style");
document.head.appendChild(s);
changeCursor(r)
test.addEventListener("input", ()=>{
r = test.value;
changeCursor(r)
})
function changeCursor(r){
let rule = `cursor: url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r="${r}" stroke-width="2" style="stroke: black; fill: red;"/></svg>') 10 10, pointer;`
s.textContent = `.brushCursor { ${rule} }`;
}
.brushCursor {
height:100vh;
/*cursor: url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r="3" stroke-width="2" style="stroke: black; fill: red;"/></svg>') 10 10, pointer;*/
}
#test{position:absolute; top:1em;}
<div class="brushCursor"></div>
<input id="test" type="range" min="2" max="7" value="3" />
为了在滚动时更改它,我需要了解更多信息.我使用的输入的最大值 = 7.也许 8 可以;然而,更大的半径会给你一个切割圆.您究竟希望如何在滚动时扩大您的圈子.页面也有不同的高度.是滚动"还是轮转"您计划使用的事件?
In order to change it on scroll I would need to know more. The input I'm using has a max = 7. Maybe 8 would do; however a bigger radius would give you a cuted circle. How exactly would you want to grow your circle on scroll. Also pages have different heights. Is it "scroll" or is it "wheel" the event you plan to use?
这篇关于动态改变SVG光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!