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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<div class="container">
<!-- where the IPM goes -->
<div id="my_IPM"></div>
</div>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Load color palettes, for that viridis fun -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
.container {
max-width: 800px;
margin: 0 auto;
}
var matdim = 800; // set this to be the same width as the container
// set the dimensions and margins of the graph
var margin = {
top: 50,
right: 50,
bottom: 50,
left: 50
},
width = matdim - margin.left - margin.right,
height = matdim - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_IPM")
.append("div")
// Container class to make it responsive.
.classed("svg-container", true)
.append("svg")
// Responsive SVG needs these 2 attributes and no width and height attr.
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", "0 0 "+matdim+" "+matdim)
// Class to make it responsive.
.classed("svg-content-responsive", true)
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// generate data
// I generate random numebrs for this but you could give values
// survival probability intercept and slop
survint = Math.random() - 3;
survz = Math.random();
// growth kernel intercept, slope and sd
growint = Math.random() + 1;
growz = 1 - growint / 2;
growsd = Math.random() / 3 + 0.1;
// probability of reproducing intercept and slop
reprint = Math.random() - 3;
reprz = Math.random();
// number of offspring intercept and slope
noffint = Math.random();
noffz = Math.random();
// recruit size intercept and slop
rcszint = 0.75 + Math.random() / 2;
rcszsd = Math.random() / 3 + 0.1;
// dnorm function
var pi = 3.1415926;
var e = 2.71828;
function dnorm(x, mean, sd) {
return 1 / Math.sqrt(2 * pi * Math.pow(sd, 2)) * Math.pow(e, -(Math.pow(x - mean, 2) / (2 * Math.pow(sd,
2))))
}
// survival function (logit transformation)
function survFunction(z) {
linear = survint + survz * z
return 1 / (1 + Math.pow(e, -linear))
};
// growth function
function growthFunction(z, z1) {
mu = growint + growz * z; // mean size next year
sig = growsd; // sd about mean
pdengrow = dnorm(z1, mean = mu, sd = sig); // pdf that you are size z1 given you were size z
return pdengrow; // The function returns the product of p1 and p2
};
// probability of reproduction function (logit transformation)
function reprFunction(z) {
linear = reprint + reprz * z
return 1 / (1 + Math.pow(e, -linear))
};
// number of offspring function (exponential)
function noffFunction(z) {
linear = noffint + noffz * z
return Math.pow(e, linear) * 0.1
};
// size at birth function
function rcszFunction(z1) {
mu = rcszint; // mean size next year
sig = rcszsd; // sd about mean
pdengrow = dnorm(z1, mean = mu, sd = sig); // pdf that you are size z1 given you were size z
return pdengrow;
}
// survival and fecundity kernels
function Pkern(z, z1) {
return survFunction(z) * growthFunction(z, z1)
}
function Fkern(z, z1) {
return reprFunction(z) * noffFunction(z) * rcszFunction(z1)
}
let numbers = [];
let numbers2 = [];
let result = [];
var csvfile = "group,variable,value\n"
// the kernel resolution
kerndim = 100;
// maximum and minimum suze range (L and U)
maxsize = 3;
minsize = 0;
// loop through
for (let i = 0; i < (kerndim * kerndim); i++) {
numbers.push(((i % kerndim) + 1) / kerndim * maxsize);
numbers2.push((Math.floor((i / kerndim) + 1)) / kerndim * maxsize);
// calculate the kernel value (P+F)
result.push(Pkern(numbers[i], numbers2[i]) + Fkern(numbers[i], numbers2[i]));
csvfile = csvfile.concat("X", numbers[i], ",Y", numbers2[i], ",", result[i], "\n");
}
// make it into a csv
var data = d3.csvParse(csvfile);
// check it out
// it's an array with dimension= kerndim * kerndim (eg. long format with columns: x,y,value)
console.log(data);
// Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
var myGroups = d3.map(data, function (d) {
return d.group;
}).keys()
var myVars = d3.map(data, function (d) {
return d.variable;
}).keys()
// Build X scales and axis:
var x = d3.scaleBand()
.range([0, width])
.domain(myGroups)
.padding(0);
// Build Y scales and axis:
var y = d3.scaleBand()
.range([height, 0])
.domain(myVars)
.padding(0);
// Build color scale
var myColor = d3.scaleSequential()
.interpolator(d3.interpolateViridis)
.domain([0, Math.max(...result)])
// add the squares
svg.selectAll()
.data(data, function (d) {
return d.group + ':' + d.variable;
})
.enter()
.append("rect")
.attr("x", function (d) {
return x(d.group)
})
.attr("y", function (d) {
return y(d.variable)
})
.attr("rx", 0)
.attr("ry", 0)
.attr("width", x.bandwidth() + 1)
.attr("height", y.bandwidth() + 1)
.style("fill", function (d) {
return myColor(d.value)
})
.style("stroke-width", 4)
.style("stroke", "none")
.style("opacity", 1)
// x axis
var x = d3.scaleLinear()
.domain([minsize, maxsize]) // This is what is written on the Axis: from 0 to 100
.range([0, width]);
svg
.append("g")
.attr("transform", "translate(0,"+height+")") // This controls the vertical position of the Axis
.call(d3.axisBottom(x));
// yaxis
var y = d3.scaleLinear()
.domain([maxsize, minsize]) // This is what is written on the Axis: from 0 to 100
.range([0, width]);
svg
.append("g")
.attr("transform", "translate(0,0)") // This controls the vertical position of the Axis
.call(d3.axisLeft(y));
// add the x axis label
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top) + ")")
.style("text-anchor", "middle")
.style("font-style","italic")
.style("font-size", "35px")
.text("z");
// y axis label
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left-15)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.style("font-style","italic")
.style("font-size", "35px")
.text("z'");
Also see: Tab Triggers