Activate your free membership today | Log-in

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:

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

And here for canvas:

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

Exciting!

Posted by Dion Almaer at 7:40 am
3 Comments

+++--
3.5 rating from 14 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.