Tuesday, July 15th, 2008

Paint Servers: SVG and Canvas

Category: Canvas, SVG

Robert O’Callahan and Dave Hyatt have been chatting about paint servers as Robert creates SVG ones and then arbitary elements.

This continues the meme of taking common use cases and making them easy via CSS (e.g. reflections).

Here is the SVG version:

  1. <html xmlns="http://www.w3.org/1999/xhtml"
  2.     xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <style>
  5. h1 { background:url(#h); }
  6. p { background:url(#p); }
  7. span { background:url(#h); }
  8. </style>
  9. </head>
  10. <body>
  11.   <h1 style="width:95%;">Heading</h1>
  12.   <p style="width:90%; border:1px solid black; margin-left:2em;">
  13.   "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
  14.   incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
  15.   exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
  16.   irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
  17.   pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
  18.   deserunt mollit anim id est laborum."
  19.   </p>
  20.   <div style="width:200px;">"Lorem ipsum dolor sit amet,
  21.   <span>consectetur adipisicing elit, sed do eiusmod</span>
  22.   tempor incididunt ut labore et dolore magna aliqua.</div>
  23.   <svg :svg style="height:0">
  24.     </svg><svg :linearGradient id="h" x2="1" y2="0">
  25.       <svg :stop stop-color="yellow" offset="0"/>
  26.       <svg :stop stop-color="yellow" stop-opacity="0" offset="1"/>
  27.     </svg>
  28.     <svg :pattern id="p" patternUnits="userSpaceOnUse"
  29.             x="0" y="0" width="50" height="50"
  30.             viewBox="-1 -1 9 5.5" >
  31.       <svg :path d="M 0 0 L 7 0 L 3.5 7 z" fill="red" stroke="blue" opacity="0.3"/>
  32.     </svg>
  33.  
  34. </body>
  35. </html>

And here for canvas:

  1. < !DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <style>
  5. p { background:url(#d); }
  6. </style>
  7. </head>
  8. <body style="background:yellow;">
  9.   <p style="width:60%; border:1px solid black; margin-left:100px;">
  10.   "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
  11.   incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
  12.   exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
  13.   irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
  14.   pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
  15.   deserunt mollit anim id est laborum."
  16.   </p>
  17.   <canvas id="d" width="50" height="50"></canvas>
  18.   <script>
  19.     var d = document.getElementById("d");
  20.     var iteration = 0;
  21.     function iterate() {
  22.       ++iteration;
  23.       var ctx = d.getContext("2d");
  24.       ctx.save();
  25.       ctx.clearRect(0, 0, 50, 50);
  26.       ctx.translate(25,25);
  27.       ctx.rotate(Math.PI*iteration/180);
  28.       ctx.fillStyle = "lime";
  29.       ctx.fillRect(-10, -10, 20, 20);
  30.       ctx.restore();
  31.       setTimeout(iterate, 10);
  32.     }
  33.     iterate();
  34.   </script>
  35. </body>
  36. </html>

Exciting!

Posted by Dion Almaer at 7:40 am
3 Comments

+++--
3.4 rating from 19 votes

3 Comments »

Comments feed TrackBack URI

Hopefully someone will post and elaborate on the 10 words that were rudely thrown together to make what appears to be a completely pointless article.

I mean seriously, there is no substance to this post. Ajaxian needs to re-evaluate their staff.

Comment by seancallan — July 15, 2008

A paint server is an SVG concept used to generate fills: http://www.w3.org/TR/1999/WD-SVG-19990211/painting.html

Comment by Joeri — July 16, 2008

I agree with Sean — thanks for the code samples, I guess.

Comment by MichaelThompson — July 16, 2008

Leave a comment

You must be logged in to post a comment.