3

So I have a onChange listener on an input type image which triggers the appending of an image element onto the svg. The problem comes when i try to scale the image with viewbox or other methods. The image element is attached to the svg but it only covers a portion of the svg equivalent to the image resolution. I want the image to be scaled accordingly within the svg.

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            var svg = document.getElementById('svg');
            $("svg").empty();

            var image = document.createElementNS(svgNS, 'image');
            image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', e.target.result);
            image.setAttributeNS(svgNS, 'class', 'map-image');
            image.setAttributeNS(svgNS, 'width', '1000px');
            image.setAttributeNS(svgNS, 'height', '500px');
            image.setAttributeNS(svgNS, 'x', '0');
            image.setAttributeNS(svgNS, 'y', '0');
            svg.appendChild(image);
        }

        reader.readAsDataURL(input.files[0]);
    }
}
  $('#input-map').on('change', function () {
        //get the file name
        var fileName = $(this).val();
        readURL(this)
        //replace the "Choose a file" label
        $(this).next('.custom-file-label').html(fileName);
    })
Alex
  • 71
  • 1
  • 6
  • 1
    Did you try transform:scale();? – Dev Kosov Jan 20 '20 at 12:58
  • Why would `viewBox` scale anything? The viewbox attribute only says which part of the infinite SVG document should count as "renderable content". But a more pertinent question is: why are you using an SVG element at all? Is there something that `` can't do here? – Mike 'Pomax' Kamermans Jan 20 '20 at 14:57

0 Answers0