简体   繁体   English

使用D3用背景图像填充svg

[英]Using D3 to fill an svg with a background image

I've posted a few other questions about this and have now abandoned my previous bootstrap framework for a solid svg strip using D3. 我已经发布了一些关于此问题的其他问题,现在已经放弃了我之前使用D3的固体svg条带的bootstrap框架。

My goal is to have 3 triangles masking 3 images, that are clickable to page anchor links only inside the triangles. 我的目标是让3个三角形屏蔽3个图像,这些图像可以单击以仅在三角形内部页面锚定链接。 (Ideally I want to add a transition-to-circle effect on hover also, but I'm not worried about that now). (理想情况下,我也希望在悬停时添加过渡到圆圈的效果,但我现在并不担心这一点)。

I have the jsfiddle below so far, but can't manage to un-rotate the images inside the triangle, or make the background be just one single image instead of the cover like it is now. 到目前为止,我有下面的jsfiddle,但无法在三角形内部取消旋转图像,或者使背景只是一个单独的图像而不是现在的封面。 I tried CSS background-image also, but with no success. 我也试过CSS背景图像,但没有成功。

Here's a piece of my d3.js code, and a full jsfiddle below. 这是我的d3.js代码,下面是一个完整的jsfiddle。

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

var defs= svg.append('defs')

defs.append('pattern')
    .attr('id', 'pic1')
    .attr('patternUnits', 'userSpaceOnUse')
    .attr('width', 100)
    .attr('height', 100)
  .append('svg:image')
    .attr('xlink:href', 'http://placehold.it/1749x1510')
    .attr("width", 100)
    .attr("height", 100)
    .attr("x", 0)
    .attr("y", -10);

svg.append("a")
    .attr("xlink:href", "http://www.google.com")
    .append('path')
    .attr("overflow","hidden")
    .attr("d", d3.svg.symbol().type("triangle-up").size(10000))
    .attr("transform", function(d) { return "translate(" + 300 + "," + 200 + ") rotate(0)"; })
    .attr("fill", "url(#pic1)");

http://jsfiddle.net/5Ldzk5w6/2/ http://jsfiddle.net/5Ldzk5w6/2/

Thank you for any time or help you can give to fix those images! 感谢您随时或帮助您修复这些图片!

If you want the patterns to fill the triangle, make them the same size as the triangle. 如果希望图案填充三角形,请将它们设置为与三角形相同的尺寸。 Your pattern was 100x100, but your triangles were much bigger than that. 你的模式是100x100,但是你的三角形比那个大得多。 So the pattern was repeating. 所以模式重复了。

If you don't want your pattern fill to be rotated, don't rotate your triangle. 如果您不希望旋转图案填充,请不要旋转三角形。

If you don't want the images in your pattern squashed, define your pattern so it has the same aspect ratio. 如果您不希望图案中的图像被压扁,请定义图案,使其具有相同的纵横比。 Your images are rectangular, but you were squishing them into a square shape (100x100). 你的图像是矩形的,但你正在将它们压成方形(100x100)。

Here's a fixed demo sample below. 这是下面的固定演示示例。 Complete fiddle here 在这里完成小提琴

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://cammac7.github.io/img/portfolio/LRO.jpg')
    .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)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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