Book Image

Learning Raphaël JS Vector Graphics

By : Damian Dawber
Book Image

Learning Raphaël JS Vector Graphics

By: Damian Dawber

Overview of this book

<p>Raphaël is an exceptional library that makes drawing vector graphics in the browser straightforward. It boasts a large number of methods that facilitate drawing and animating graphics, allowing developers to create flexible, interactive web applications and data visualizations.<br /><br />Learning Raphaël JS Vector Graphics takes you from being a complete vector graphics novice to an accomplished vector graphics developer. Packed with illustrations and code demos, this book covers a wide array of concepts and takes you through them by example. The Raphaël library is covered in detail and in the context of its real-world applicability. <br /><br />This book looks at the powerful vector graphics drawing library, Raphaël, and how you can utilize it to draw vector graphics and create interactive web applications with ease.</p> <p><br />You will learn how to draw complex vector graphics and how to transform, animate, and interact with them. We will also look at working with existing vector graphics to add an extra layer of complexity to our applications, and finish up by creating a series of data visualization demos. If you want to learn how to create appealing, interactive graphics and data visualizations, then this is the book for you.</p> <p><br />Learning Raphaël JS Vector Graphics is packed full of illustrations and has over 70 demos to really hammer home the concepts covered.</p>
Table of Contents (14 chapters)

Working with text


Drawing text in the canvas rather than as HTML markup with CSS styling allows us to animate and transform it in the same way as we would for other shapes. Text is created using the text method and its properties (such as size and font-family) are modifiable as attributes. The text method has the following definition, where the text parameter is our text string and accepts the standard escape sequence '\n' as input, which places the proceeding text onto a new line:

Paper.text(x, y, text)

The following code creates two pieces of text using this method and then groups them into a set in order to apply font-size and font-family attributes to both elements:

var text1 = paper.text(0, 15, 'I am text anchored start.');
text1.attr({
    'text-anchor': 'start',
});
var text2 = paper.text(270, 100, 'I am text\nanchored middle');
text2.attr({
    'text-anchor': 'middle'
});
var group = paper.set(text1, text2);
group.attr({
    'font-size': 20,
    'font-family': 'serif'
});

You should...