Jody Heavener
Jody Heavener

Reputation: 2874

Troubles with masking in Snap.svg

I'm trying to use Snap.svg to create a glyph icon (it will be animated later, but for now just the icon). It should look like this:

I've exported the paths for each facial feature. Then, using Snap, I've created a white circle and added each path to the element:

var snapFace = Snap('#face');

var faceCircle = snapFace.circle(17.075, 17.075, 17.075);
faceCircle.attr({
  fill: '#fff'
});

var faceLeftEye = snapFace.path('M34.4302501,33.1788512 C35.6480686,33.1788512 36.6363606,34.1675226 36.6363606,35.3819266 C36.6363606,36.6008833 35.6480686,37.5876578 34.4302501,37.5876578 C33.2162254,37.5876578 32.2313479,36.6008833 32.2313479,35.3819266 C32.2313479,34.1679019 33.2162254,33.1788512 34.4302501,33.1788512 L34.4302501,33.1788512 Z');
faceLeftEye.attr({
  transform: 'translate(-23.08, -22.81)',
  fill: '#fff'
});

var faceRightEye = snapFace.path('M34.4302501,33.1788512 C35.6480686,33.1788512 36.6363606,34.1675226 36.6363606,35.3819266 C36.6363606,36.6008833 35.6480686,37.5876578 34.4302501,37.5876578 C33.2162254,37.5876578 32.2313479,36.6008833 32.2313479,35.3819266 C32.2313479,34.1679019 33.2162254,33.1788512 34.4302501,33.1788512 L34.4302501,33.1788512 Z');
faceRightEye.attr({
  transform: 'translate(-11.08, -22.81)',
  fill: '#fff'
});

var faceMouth = snapFace.path('M40.2511192,53.2459206 C33.6627589,53.2459206 28.3195327,49.6857932 28.2884233,41.3393734 L52.208883,41.3393734 C52.183085,49.6857932 46.8387207,53.2459206 40.2511192,53.2459206 L40.2511192,53.2459206 Z');
faceMouth.attr({
  transform: 'translate(-23.08,-22.81)',
  fill: '#fff'
});

From there I grouped the three facial features and set them as the mask attribute on the circle:

var maskGroup = snapFace.group(faceLeftEye, faceRightEye, faceMouth);

faceCircle.attr({
  mask: maskGroup
});

I expected at this point for everything to come together. Unfortunately it looks like this:

I know the face's outer circle is in the right place as I can find it in the inspector; it's just masked. I can't figure out how to get the inverse to happen, where the facial features subtract from the outer circle.

If anyone has advice I'd really appreciate the help!

Upvotes: 0

Views: 158

Answers (1)

Jody Heavener
Jody Heavener

Reputation: 2874

I should've looked harder. Found the answer here: An SVG rectangle with multiple holes using Snap.svg -- specifically this Pen.

Basically I had to add an additional circle to the mask, with a white fill, then set the facial feature fills to black.

var snapFace = Snap('#face');

var faceCircle = snapFace.circle(17.075, 17.075, 17.075);
faceCircle.attr({ fill: '#fff' });

var maskGroup = snapFace.group();
maskGroup.circle(17.075, 17.075, 17.075).attr({ fill: '#fff' });

var faceLeftEye = snapFace.path('M34.4302501,33.1788512 C35.6480686,33.1788512 36.6363606,34.1675226 36.6363606,35.3819266 C36.6363606,36.6008833 35.6480686,37.5876578 34.4302501,37.5876578 C33.2162254,37.5876578 32.2313479,36.6008833 32.2313479,35.3819266 C32.2313479,34.1679019 33.2162254,33.1788512 34.4302501,33.1788512 L34.4302501,33.1788512 Z');
faceLeftEye.attr({ transform: 'translate(-23.08, -22.81)', fill: '#000' });
maskGroup.append(faceLeftEye);

var faceRightEye = snapFace.path('M34.4302501,33.1788512 C35.6480686,33.1788512 36.6363606,34.1675226 36.6363606,35.3819266 C36.6363606,36.6008833 35.6480686,37.5876578 34.4302501,37.5876578 C33.2162254,37.5876578 32.2313479,36.6008833 32.2313479,35.3819266 C32.2313479,34.1679019 33.2162254,33.1788512 34.4302501,33.1788512 L34.4302501,33.1788512 Z');
faceRightEye.attr({ transform: 'translate(-11.08, -22.81)', fill: '#000' });
maskGroup.append(faceRightEye);

var faceMouth = snapFace.path('M40.2511192,53.2459206 C33.6627589,53.2459206 28.3195327,49.6857932 28.2884233,41.3393734 L52.208883,41.3393734 C52.183085,49.6857932 46.8387207,53.2459206 40.2511192,53.2459206 L40.2511192,53.2459206 Z');
faceMouth.attr({ transform: 'translate(-23.08,-22.81)', fill: '#000' });
maskGroup.append(faceMouth);

faceCircle.attr({ mask: maskGroup });

Upvotes: 1

Related Questions