简体   繁体   中英

change circle svg element to path with js manipulation

totally new to svg and js mixed together, would like some help here.

I'm using this script in my website: https://www.techdezine.com/background-blur-image-revealed-with-mouse-action/

I'm trying to change the circle shape (using < circle > element).

to a diamond shape (or probably path element) ( link to example ), so the javascript will still update the mouse location coordinates of the different shape

it contains this html

<div class="pic">
<svg class="blur" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%">
    <image filter="url(#filter2)" xlink:href="female.jpg" width="100%" height="100%"></image>
    <filter id="filter2">
        <feGaussianBlur stdDeviation="5" />
    </filter>
    <mask id="mask1">
        <circle cx="-50%" cy="-50%" r="40" fill="white" filter="url(#filter2)" />
    </mask>
    <image xlink:href="female.jpg" width="100%" height="100%" mask="url(#mask1)"></image>
</svg>
</div> 

and is being manipulated using this JS

$('.pic').mousemove(function (event) {
    event.preventDefault();
    var upX = event.clientX;
    var upY = event.clientY;
    var mask = $('#mask1 circle')[0];
    mask.setAttribute("cy", (upY - 5) + 'px');
    mask.setAttribute("cx", (upX) + 'px');
});

any ideas how can it be done, Im too long on that, and still dont have a clue.

The solution

instead of setAttributes for CY and CX i setted the transform="translate()" attribute. it worked perfectly.

$('.pic').mousemove(function (event) {
 event.preventDefault();
 var upX = event.clientX;
 var upY = event.clientY;
 var mask = $('#mask1 circle')[0];
 var mask = jQuery('#mask1 polygon')[0];
 mask.setAttribute("transform", "translate(" + (upX) + ',' + (upY) + ')' );
});

and the HTML

<mask id="mask1">
        <polygon fill="white" points="69.445,125 125,28.774 180.556,125 
    125,221.227 " transform="translate(-125,-125)"/>

    </mask>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM