What is the best way to manipulate a shape in Snap.svg while ensuring the transformation does not affect its mask?

My current challenge involves transforming a rectangle with position, scale, and angle adjustments while having it masked.

To illustrate my issue, I have created a fiddle file which can be accessed via this link: http://jsfiddle.net/MichaelSel/vgw3qxpg/2/

In the demonstration, when you click on the green rectangle, observe how it moves in relation to the stationary circle positioned above it. This behavior aligns with my desired outcome for these objects.

However, by clicking the button above, the circle now functions as a mask for the green rectangle. Subsequently, any transformations applied cause the circle (mask) to move as well, instead of remaining fixed like before.

While this is a common approach with masks, my application requires the opposite behavior.

I managed to address this dilemma by implementing a contrasting movement in the blue example.

Despite the simplicity of my demo, I aim to incorporate this functionality into a larger application. Is there a more straightforward way to achieve my desired result without encountering similar complexities?

The following code snippet pertains to the green (undesirable) example:

  s = Snap("#player")
    rect = s.rect(100, 100, 100, 100).attr({"fill" : "green" })
    circle = s.circle(200, 200, 50).attr({"fill" : "white" })
    rect.attr("mask",circle)
    rect.click(function () {
        rect.transform("t20,10s1.1...")
    })

Below is the code for the blue (preferred) example:

    rect2 = s.rect(300, 100, 100, 100).attr({ "fill": "blue" })
    circle2 = s.circle(400, 200, 50).attr({ "fill": "white" })
    rect2.attr("mask", circle2)
    rect2.click(function () {
        rect2.transform("t20,10s1.1...")
        circle2.transform("t-20,-10s"+1/1.1 + "...")
    })

Do you have any suggestions or insights that could assist me in resolving this issue? Your help would be greatly appreciated.

Answer №1

Create a rectangle and place it in a group. By applying the mask to the group, you're able to freely transform the rectangle without affecting the mask.

var s = Snap("#player");
var rect = s.rect(100, 100, 100, 100).attr({"fill": "green"});
var g = s.group(rect);
var circle = s.circle(200, 200, 50).attr({"fill": "white"});
rect.click(function () {
    rect.transform("t20,10s1.1...");
});

$("#myButton").click(function () {
    g.attr("mask", circle);
});

Click here for an example on JSFiddle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generate a div element dynamically when an option is selected using AngularJS

I'm having trouble dynamically creating div elements based on the selected option value, but for some reason ng-repeat isn't working as expected. Can you help me figure out what I'm missing? Here's the HTML snippet I'm using - &l ...

Using ID attributes to compare JavaScript Objects and generate a distinct list within Angular/JS

I am working with the github API to retrieve an array of objects containing commit data. My goal is to extract a unique list of commitors and format it as shown below to create a graph: {"id":temp,"label":temp,"type": "number","p": {} } Here is the code ...

Adjust the size of the embedded iframe to match the dimensions of the containing div

Is there a method to resize the iframe embed code for a vine so that it fits within the boundaries of the enclosing div? The supplied embed code already includes sizing information as shown below: <iframe src="https://vine.co/v/iMJzrThFhDL/embed/simp ...

sending an alert via a function's return statement

Could you explain to me why this code isn't functioning properly? When I call s.A, the alert message doesn't appear. Can you help me understand why? var s = { A: function () { alert("test A"); }, B: function () { alert("test B"); } }; ...

Is it possible to utilize AJAXToolKit and Jquery on a single webpage?

Is it possible to utilize both AJAXToolKit and jQuery on a single page? For example, let's say we have ScriptManager in the same page along with including ...

The function fails to return a true value

Why is true never returned even when the given name exists in the array rows and the if(rows[i].userName == name) condition is met? function checkIfUserExists(name){ var isUserExists = false; OOTW.MYSQL.query('SELECT * FROM Time',functio ...

What is the best way to wrap a call to XMLHttp Request within a $q promise?

I am trying to figure out how to wrap the following code in an AngularJS ui-router resolve section with a $q promise. The goal is to return success or fail based on whether the http request works. Should I encapsulate all of this inside a promise or just ...

Can JavaScript be used to bypass AJAX cookies?

Is there a way in JavaScript to programmatically ignore server-sent cookies without adjusting browser settings? We have implemented plugins on our web server that periodically update our security cookie, resulting in compatibility issues with certain URLs ...

"Building a dynamic form with ReactJS, Redux Form, and Material UI - Implementing an

Currently working on a nested form framework that utilizes the redux form and material UI framework. The components have been developed up to this point - https://codesandbox.io/s/bold-sunset-uc4t5 My goal is to incorporate an autocomplete field into the ...

React - Error occurred when parsing module: Unexpected Token. It is recommended to use a suitable loader to manage this file format

As a beginner in using React, I might be making obvious mistakes and I apologize for that. After researching similar bugs on various platforms like StackOverflow and GitHub, I couldn't find a solution that works for my specific issue. Here is the erro ...

Utilizing jQuery to modify the text output from an input form

Check out the preview site at . It's a test version before launching on a separate domain. Hello, I need to customize the error response for a CRM contact form integrated into Wordpress with the help of StackOverflow. The form is connected via JavaSc ...

Analyze the elements within arrays and determine the occurrence rate of each element

Issue at hand: var rawData = [[two],[three],[four],[one],[two],[two],[two],[three],[four],[five],[three],[four],[five]]; Given an array of arrays, the goal is to identify the unique elements within one of the arrays and provide a count of each unique ele ...

What are the steps to resolve the issue of encountering the error message "Unrecognized custom element: <bot-ui>"?

I'm having some trouble setting up a chatbot screen with the "botui" and "vue-cli". Whenever I try to display the screen, I encounter an "Unknown custom element: " error. Below is the code snippet that I am using. Can you please help me figure out ...

jQuery Issue DetectedSlight complication spotted with jQuery

I've encountered a peculiar problem with jQuery's contains function: HTML <span class="tag diaTags label label-info">Teststring<span data-role="remove"></span></span> JS When I directly use $('span.diaTags:contai ...

Is there a way to reset a state without needing to declare an initialState beforehand?

I'm facing a situation where I need to reset a state without having to create an initial state again. Here's the dilemma: initialState: { id: '', name: '', index: '' }, state: { ...

Iterate over each item in the select and include a blank option if either of the elements is missing

Having trouble with 2 drop down menus, select1 and select2. I select item1 from select1 and then click the OK button. But when the selected index is 0 (first option), I want to loop through the items of both drop downs. If the elements at the same index ( ...

Transferring data via AJAX technology

My goal is to empower the ability to upload files using AJAX. I attempted to utilize (JavaScript) in this manner: $("input[type='file']").change(function(){ var file = document.getElementById("uploadelement").files[0]; $.ajax ...

Having trouble modifying a value in a form and submitting it to the following jsp page

I'm encountering an issue with changing the value in a hidden input element before submitting data to another JSP file (db.jsp) for processing. The value should be different depending on which button is clicked, but it always remains as the default va ...

Unable to see or play local mp4 file in Ionic iOS application

I am facing an issue with my Ionic app where I am trying to show a video playing in the background. The Jaeger25 HTMLVideo plugin works perfectly for Android, but when it comes to iOS, the video simply refuses to play. The video file is located in the www/ ...

Create a customized menu with jQuery that disappears when hovered over

Check out this menu I've created: http://jsbin.com/useqa4/3 The hover effect seems to be working fine, but I'm looking to hide the div #submenuSolutions when the user's cursor is not on the "Solution" item or the submenu. Is there a way to ...