SVG path drawing and filling with image

singmotor

I need a little bit of help understanding/editing some svg paths. I was assisted in drawing three identical triangles (with images inside) and I want to change the first two triangles to one contiguous parallelogram. I have played with the numbers a ton and finally got the shape I want, but the image always repeats at a line partway into the middle of the shape drawn by the path. I changed the aspect ratio of the image a bunch but to no avail. Can anyone tell me what I'm doing wrong? Here is an example jsFiddle and my code.

I want what's there, but need the parallelogram to be the same height as the middle triangle (thereby covering it completely), and I need it to not have the image repeat. Any explanation of how these different .attr are affecting things would be greatly helpful from an education perspective.

var width = 800;
var height = 200;


var svg = d3.select(".mydiv").append("svg")
                             .attr("width",width)
                             .attr("height",height)
                             .attr("viewBox", "0 0 250 100");

var defs= svg.append('defs')
defs.append('pattern')
    .attr('id', 'pic1')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 115.5)
    .attr('height', 100)
  .append('svg:image')
    .attr('xlink:href', 'http://francomasoma.com/wp-content/uploads/2016/05/FullSizeRender_7-560x483.jpg')
    .attr("width", 115.5)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", 0);


  defs.append('pattern')
    .attr('id', 'pic2')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 115.5)
    .attr('height', 100)
   .append('svg:image')
    .attr('xlink:href', 'http://wire.kapitall.com/wp-content/image-import/92277513-560x483.jpg')
    .attr("width", 115.5)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", 0);


  defs.append('pattern')
    .attr('id', 'pic3')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 115.5)
    .attr('height', 100)
    .append('svg:image')
    .attr('xlink:href', 'http://gallery.ksotov.co.uk/main.php?g2_view=core.DownloadItem&g2_itemId=13225&g2_serialNumber=1')
    .attr("width", 115.5)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", 0);



svg.append("a")
    .append('path')
    .attr("d", "M 0,0, 57.7,-100, 115.5,0z")
    .attr("transform", "translate(135.5, 100)")
    .attr("fill", "url(#pic1)");

svg.append("a")
    .append('path')
    .attr("d", "M 57.7,0, 0,-100, 115.5,-100z")
    .attr("transform", "translate(67.7, 100)")
    .attr("fill", "url(#pic2)");

svg.append("a")
    .append('path')
    .attr("d", "M 0,0, 57.7,-100, 183,-100, 125.5,0")
    .attr("transform", "translate(0, 100)")
    .attr("fill", "url(#pic3)");
Robert Longson

One way is simply to scale the image using patternTransform. I've also adjusted the height to ensure it is placed correctly.

var width = 800;
var height = 200;
 

var svg = d3.select(".mydiv").append("svg")
                             .attr("width",width)
                             .attr("height",height)
                             .attr("viewBox", "0 0 250 100");

var defs= svg.append('defs')
defs.append('pattern')
    .attr('id', 'pic1')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 115.5)
    .attr('height', 100)
  .append('svg:image')
    .attr('xlink:href', 'http://francomasoma.com/wp-content/uploads/2016/05/FullSizeRender_7-560x483.jpg')
    .attr("width", 115.5)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", 0);


  defs.append('pattern')
    .attr('id', 'pic2')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 115.5)
    .attr('height', 100)
   .append('svg:image')
    .attr('xlink:href', 'http://wire.kapitall.com/wp-content/image-import/92277513-560x483.jpg')
    .attr("width", 115.5)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", 0);


  defs.append('pattern')
    .attr('id', 'pic3')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 115.5)
    .attr('height', 75)
    .attr('patternTransform', "scale(2)")
    .append('svg:image')
    .attr('xlink:href', 'http://gallery.ksotov.co.uk/main.php?g2_view=core.DownloadItem&g2_itemId=13225&g2_serialNumber=1')
    .attr("width", 115.5)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", 0);



svg.append("a")
    .attr("xlink:href", "http://www.google.com")
    .append('path')
    .attr("d", "M 0,0, 57.7,-100, 115.5,0z")
    .attr("transform", "translate(135.5, 100)")
    .attr("fill", "url(#pic1)");

svg.append("a")
    .attr("xlink:href", "http://www.wikipedia.com")
    .append('path')
    .attr("d", "M 57.7,0, 0,-100, 115.5,-100z")
    .attr("transform", "translate(67.7, 100)")
    .attr("fill", "url(#pic2)");

svg.append("a")
    .attr("xlink:href", "#testanchor")
    .append('path')
    .attr("d", "M 0,0, 57.7,-100, 183,-100, 125.5,0")
    .attr("transform", "translate(0, 100)")
    .attr("fill", "url(#pic3)");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div class="mydiv">
    </div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SVG path drawing and filling with image

From Dev

Filling a path depends on the drawing order

From Dev

filling svg background with an image

From Dev

Filling an SVG path with multiple colors

From Dev

SVG path drawing truncated in Firefox

From Dev

Path not filling the full SVG element size

From Dev

Filling a round SVG element with an image in javascript

From Dev

Filling a round SVG element with an image in javascript

From Dev

Filling a rotating circular SVG with a pattern image

From Dev

hand drawing (crayon) style for SVG path?

From Dev

svg path not drawing for single point on chrome surface

From Dev

SVG Scaling the filling-pattern of an path to the paths size

From Dev

RenderedImage filling or Drawing a IMAGEVIEW

From Dev

Drawing and filling in shapes

From Dev

Drawing an SVG Raphael path gives error and doesn't accept variables

From Dev

svg path gets scribling ,when drawing on line at mozila firefox

From Dev

C# .svg file to System.Drawing.Image

From Dev

Is it possible to use an image in place of the stroke of an SVG path?

From Dev

HTML SVG map image follow path

From Dev

How to create a Path in JavaFX from an SVG image?

From Dev

Moving image on scroll through svg path

From Dev

Path is not filling with color

From Dev

Drawing on Image

From Dev

Libgdx Filling Screen With Image

From Dev

Tricky filling holes in an image

From Dev

Image Boundary Finding and Filling

From Dev

Filling an entire MenuButton with an Image

From Dev

SVG HTML Not Drawing SVG Pattern

From Dev

Outlining and partially filling an SVG Shape