2016-07-23 240 views
1

我需要一點幫助理解/編輯一些svg路徑。我協助繪製了三個相同的三角形(裏面有圖像),我想將前兩個三角形改爲一個連續的平行四邊形。我已經玩了數噸,最終得到了我想要的形狀,但圖像總是重複在路徑所畫的形狀中間的一條線上。我改變了圖像的縱橫比,但無濟於事。誰能告訴我我做錯了什麼?這裏是一個例子jsFiddle和我的代碼。SVG路徑繪製和圖像填充

我想要什麼,但需要平行四邊形與中間三角形高度相同(從而完全覆蓋它),並且我需要它不會重複圖像。從教育的角度來看,對這些不同的.attr如何影響事物的任何解釋都會很有幫助。

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)"); 

回答

2

一種方法是簡單地使用patternTransform來縮放圖像。我也調整了高度以確保它的放置正確。

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>