2016-10-11 9 views

答えて

2

これは、JavaScriptの質問は、よりd3.jsです:

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script data-require="[email protected]" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <svg width="100" height="100"></svg> 
 
    <script> 
 
    
 
    // set up svg image tag 
 
    var images = d3.select("svg") 
 
     .append('svg:image') 
 
     .attr('width', 50) 
 
     .attr('height', 50); 
 

 
    // create a test image 
 
    var imgTest = new Image(); 
 
    // if it loads successfully add it to the svg image 
 
    imgTest.onload = function() { 
 
     images.attr("xlink:href", imgTest.src); 
 
    } 
 
    // if it fails test another image 
 
    imgTest.onerror = function() { 
 
     imgTest.src = "https://dummyimage.com/50x50/000/fff.png&text=An+Image!" 
 
    } 
 
    // this will fail 
 
    imgTest.src = "https://does.not/exist.png"; 
 

 
    </script> 
 

 
</body> 
 

 
</html>

関連する問題