Skip to content

Instantly share code, notes, and snippets.

@MatthewBarker
Created December 15, 2016 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewBarker/97dafbbb16927e8e3aacc2d68f7ff13f to your computer and use it in GitHub Desktop.
Save MatthewBarker/97dafbbb16927e8e3aacc2d68f7ff13f to your computer and use it in GitHub Desktop.
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