HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header>
<h1>SVG Brush (first test)</h1>
<h2>Drawing SVG paths on Canvas with Photoshop-like brushes</h2>
<h3><a href="https://codepen.io/lbebber">By Lucas Bebber</a></h3>
</header>
<canvas width="400" height="250"></canvas>
<br />
<button class="button-rough">Rough Brush</button>
<button class="button-oval">Oval Brush</button>
<p>(from this SVG)</p>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="390px" height="249px" viewBox="0 0 390 249" enable-background="new 0 0 390 249" xml:space="preserve">
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M28.089,48.172c2.339,49.633-7.635,102.116-5.925,151.969
c26.925-4.92,54.728-15.152,81.318-23.453"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M114.426,104.849c-2.639,18.624-2.253,44.778,14.806,52.441
c19.732,8.863,34.095-12.15,37.325-32.19c2.042-12.674-0.671-25.136-3.49-36.345"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M222.652,78.647c-50.478-0.752-61.612,94.508,3.648,55.094"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M238.46,135.798c-0.364-22.483,14.526-52.199,25.203-74.705
c15.474,16.676,16.932,44.386,29.519,63.288"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M338.176,47.112c-20.311-0.576-63.37,24.898-29.595,35.328
c8.631,2.666,79.448-10.634,38.467,26.769c-13.665,12.47-32.776,8.171-44.138,1.982"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M226.3,108.205c19.693-2.995,44.241-11.857,64.449-18.527"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M96,226c33.96-29.328,85.695-38.628,127.121-51.798
C256.447,163.608,289.277,151.283,323,142"/>
</svg>
body
background #fafafa
text-align center
padding-left 30px
padding-right @padding-left
color #334
font-family Baskerville, Georgia, serif
a
color inherit
&:visited
color #555
header
padding-top 20px
h1,h2,h3
margin 0
margin-bottom 20px
font-weight normal
h1
font-size 3em
h3
font-size 1.2em
button
background transparent
border 1px solid #ccc
var Brush;
var BrushTip;
var Tips={};
(function() {
"use strict";
function checkObject(obj) {
return typeof obj != "object" ? {} : obj;
}
function checkNumber(number, defaultValue) {
return typeof number != "number" ? defaultValue : number;
}
Brush = function(options) {
options = checkObject(options);
this.path = options.path || null;
this.ctx = options.ctx || null;
this.spacing = checkNumber(options.spacing, 0);
this.tip = options.tip || new Tips.Rough1();
if (this.path != null)
this.pathLength = this.path.getTotalLength();
}
Brush.prototype = {
path: null,
pathLength: 0,
v: 0,
lastV: 0,
ctx: null,
spacing: 0,
tip: null,
getPos: function(v) {
v = this._cleanV(v);
return this.path.
getPointAtLength(
v * this.pathLength
);
},
draw: function() {
var v = this._getV();
this._render(v);
this.lastV = v;
},
drawFromLast: function() {
var v = this._getV();
var lastV = this.lastV;
var spacing = this.spacing;
if (spacing > 0) {
var dist = Math.abs(v - lastV);
var step = this._lenToV(spacing);
var steps = Math.ceil(dist / step);
step = dist / steps;
var curV = lastV;
var timeout=1000;
while (timeout>0 && ((v > lastV && curV <= v) || (v < lastV && curV >= v))) {
timeout--;
this._render(curV, {
rotation: 0,
pressure: Math.sqrt(1 - (Math.abs(v - 0.5) * 2))
});
curV += step;
}
}
this.lastV = v;
},
_vToLen: function(v) {
return v * this.pathLength;
},
_lenToV: function(len) {
return len / this.pathLength;
},
_getV: function() {
return this._cleanV(this.v);
},
_cleanV: function(v) {
v = checkNumber(v, this.v);
if (v > 1) v = 1;
if (v < 0) v = 0;
return v;
},
_render: function(v, options) {
v = this._cleanV(v);
options = checkObject(options);
var rotation = checkNumber(options.rotation,0);
var pressure=checkNumber(options.pressure,1);
var ctx = this.ctx;
var pos = this.getPos(v);
ctx.save();
ctx.translate(pos.x, pos.y);
this.tip.draw(ctx, {
pressure: pressure
});
ctx.restore();
}
}
BrushTip = function(options) {
options = checkObject(options);
this.size=checkNumber(options.size,1);
};
BrushTip.prototype = {
size:1,
_currentCtx: null,
_currentOptions:{},
draw: function(ctx, options) {
options = checkObject(options);
this._currentCtx = ctx;
this._render(options);
},
_render:function(options){
},
_drawCircle: function(r, x, y, polar) {
x = checkNumber(x, 0);
y = checkNumber(y, 0);
if (polar) {
var a = x;
var d = y;
x = Math.cos(a) * d;
y = Math.sin(a) * d;
}
var ctx = this._currentCtx;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fill();
ctx.closePath()
}
}
Tips.Round=function(options){
BrushTip.call(this,options);
}
Tips.Round.prototype=Object.create(BrushTip.prototype);
Tips.Round.prototype.constructor=Tips.Round;
Tips.Round.prototype._render=function(options){
BrushTip.prototype._render.call(this,options);
var pressure = checkNumber(options.pressure, 1);
var ctx=this._currentCtx;
ctx.scale(1,0.4);
this._drawCircle(6*this.size);
}
// Rough brush
Tips.Rough1=function(options){
BrushTip.call(this,options);
var maxDist=8;
this._points=new Array();
for (var i = 0; i < 15; i++) {
var d=Math.pow(Math.random(),2);
var a=Math.random()*Math.PI*2;
var r=0.1+(Math.pow(1-d,2)*4);
d*=maxDist;
this._points.push({
d:d,
a:a,
r:r
});
}
console.log(this);
}
Tips.Rough1.prototype=Object.create(BrushTip.prototype);
Tips.Rough1.prototype.constructor=Tips.Rough1;
Tips.Rough1.prototype._points=null;
Tips.Rough1.prototype._render=function(options){
BrushTip.prototype._render.call(this,options);
var pressure = checkNumber(options.pressure, 1);
var size=this.size;
var ctx=this._currentCtx;
ctx.fillStyle = "rgba(0,0,0,0.8)";
var that=this;
this._points.forEach(function(point,i){
that._drawCircle(
point.r*pressure,
point.a,
point.d,
true
);
})
}
}());
document.addEventListener('DOMContentLoaded', function() {
"use strict";
var ctx = document.
querySelector("canvas").
getContext("2d"),
svg = document.
querySelector("svg"),
paths = svg.
querySelectorAll("path"),
roughBrushes = [],
ovalBrushes = [];
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
var roughBrush = new Brush({
path: path,
ctx: ctx,
spacing: 1
});
roughBrushes.push(roughBrush);
var ovalBrush = new Brush({
path: path,
ctx: ctx,
spacing: 2,
tip:new Tips.Round()
});
ovalBrushes.push(ovalBrush);
}
function animateBrush(brush,i){
var updateBrush=function(){
brush.drawFromLast();
}
TweenMax.to(brush,1,{
v:1,
delay:0.15*i,
ease:Quint.easeOut,
onUpdate:updateBrush,
onComplete:updateBrush
})
}
function clear(){
var resetBrush=function(brush){
TweenMax.killTweensOf(brush);
brush.v=0;
brush.draw();
}
roughBrushes.forEach(resetBrush);
ovalBrushes.forEach(resetBrush);
ctx.clearRect(0,0,400,250);
}
function animateRough(){
clear();
roughBrushes.forEach(animateBrush);
}
function animateOval(){
clear();
ovalBrushes.forEach(animateBrush);
}
animateRough();
document.querySelector('.button-rough').addEventListener('click',function(){
animateRough();
});
document.querySelector('.button-oval').addEventListener('click',function(){
animateOval();
})
}, false);
Also see: Tab Triggers