Created
December 15, 2016 11:09
-
-
Save MatthewBarker/97dafbbb16927e8e3aacc2d68f7ff13f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.bindingHandlers.pathText = { | |
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { | |
var phrase = ko.unwrap(valueAccessor()); | |
var text = document.createElementNS('http://www.w3.org/2000/svg', 'text'); | |
var elementBox = element.getBBox(); | |
//text.textContent = value; | |
phrase.split(' ').forEach(function(word){ | |
var span = document.createElementNS('http://www.w3.org/2000/svg', 'tspan'); | |
span.textContent = word; | |
span.setAttribute('x', '0'); | |
span.setAttribute('dy', '1.2em'); | |
text.appendChild(span); | |
}); | |
element.parentNode.insertBefore(text, element.nextSibling); | |
var textBox = text.getBBox(); | |
var x = elementBox.x + (elementBox.width/2) - (textBox.width/2); | |
var y = elementBox.y + (elementBox.height/2) - (textBox.height/2); | |
text.setAttribute('transform', 'translate(' + x + ' ' + y + ')'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment