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.
<table id="tablo"></table>
#tablo {
width : 100%;
}
.node {
height : 40px;
width : 40px;
background-color : #009999;
color : white;
font-size : 12px;
text-align: center;
}
main {
position : absolute;
left : 0px;
right : 0px;
bottom : 0px;
top : 0px;
}
/**
*
* @author faruk can -> @farukcan.net
*
* @import Vec2.js -> farukcan.net/examples/Vec2.js
* @import Heap.js -> farukcan.net/examples/Heap.js
* @import Jquery -> cdn
* @import dat.gui -> cdn
*
* @classes Grid,Node,Astar
* @functions main
*/
$(main);
// @class Grid
function Grid(size) {
this.nodes = []
this.size = size;
this.start;this.startNode;this.target;this.targetNode;
this.create = function(){
this.nodes = [];
for (var i = 0; i < size.x; i++) {
this.nodes[i] = [];
for (var j = 0; j < size.y; j++)
this.nodes[i][j] = new Node(i, j);
}
}
this.create();
}
// @prototype Grid
Grid.prototype = {
get : function(v) {
if(this.nodes[v.x] && this.nodes[v.x][v.y])
return this.nodes[v.x][v.y];
return false;
},
setStart : function(v) {
this.start = v;
this.startNode = this.get(v);
},
setTarget : function(v) {
this.target = v;
this.targetNode = this.get(v);
}
}
// @class Node
function Node(i, j) {
this.loc = new Vec2(i, j);
this.gCost = 0;
this.hCost = 0;
this.fCost = function(){return this.gCost+this.hCost};
this.open = false;
this.closed = false;
this.blocked = false;
this.onPath = false;
this.parent;
}
// @class Astart
function Astar(grid) {
this.grid = grid;
this.gFunc = function(v) {
return this.grid.start.d(v);
}
this.hFunc = function(v) {
return this.grid.target.d(v);
}
this.getNeighbors = function(x, y) {
var neighbors = [];
neighbors.add = function(e) {
// kontrol et gerçeekten komşumu diye
if(!e) return;
if(e.blocked) return;
neighbors.push(e);
}
neighbors.add(this.grid.get(new Vec2(x, y - 1)));
neighbors.add(this.grid.get(new Vec2(x - 1, y)));
neighbors.add(this.grid.get(new Vec2(x + 1, y)));
neighbors.add(this.grid.get(new Vec2(x, y + 1)));
if (conf.allowDiagonal) {
neighbors.add(this.grid.get(new Vec2(x - 1, y - 1)));
neighbors.add(this.grid.get(new Vec2(x + 1, y - 1)));
neighbors.add(this.grid.get(new Vec2(x + 1, y + 1)));
neighbors.add(this.grid.get(new Vec2(x - 1, y + 1)));
}
delete neighbors.add;
return neighbors;
}
this.find = function() {
var OPEN = new Heap(function(a,b){
return a.fCost() - b.fCost();
});
OPEN.add(this.grid.startNode);
while (OPEN.length != 0) {
// current open içindeki en küçük f costta sahip noddür
var currentNode = OPEN.get();
if( typeof currentNode == "undefined") return false;
console.log(currentNode.loc,this.grid.target)
// current == target ise while döngüsünden çık ve createPath'ı çalıştır.
if(currentNode.loc.d(this.grid.target)==0 ){
this.createPath(this.grid.startNode,this.grid.targetNode);
return true;
}
// currenti open'dan çıkar closed'e ekle
currentNode.open = false;
currentNode.closed = true;
currentNode.gCost = this.gFunc(currentNode.loc);
var neighbors = this.getNeighbors(currentNode.loc.x,currentNode.loc.y);
// foreach komşu için
var _this = this;
neighbors.forEach(function(neighbor){
// komşu kapalıysa, bunu geç
if(neighbor.closed) return;
// komşuya gidiş = current.gcost + komşuya mesafe
var toNeighbor = currentNode.gCost + currentNode.loc.d(neighbor.loc);
neighbor.gCost = _this.gFunc(neighbor.loc);
// eğer komşu open içinde değilse veya komşuya gidiş, komşunun gcost'dan düşük ise
if(!neighbor.open || toNeighbor<neighbor.gCost){
// komşunun gcost = komşuya gidiş
neighbor.gCost = toNeighbor;
// komşunun hcost = komuşunun hedefe mesafesi
neighbor.hCost = _this.hFunc(neighbor.loc);
// komşunun parrent = current;
neighbor.parent = currentNode;
// eğer komşu open içinde değilse open'a ekle
if(!neighbor.open){
OPEN.add(neighbor);
neighbor.open = true;
}
}
})
}
return false;
}
this.createPath = function(startNode, endNode) {
var path = [];
currentNode = endNode;
while(currentNode.loc.d(startNode.loc)!=0){
path.push(currentNode);
currentNode.onPath = true;
currentNode = currentNode.parent;
}
path.reverse();
console.log(path);
return path;
}
}
// @func Map
function main() {
conf = {
sizex : 20,
sizey : 20,
allowDiagonal: false,
FIND: function() {
A.find();
},
RELOAD: function() {
clearInterval(updateval);
create();
}
}
var gui = new dat.GUI();
gui.add(conf, 'allowDiagonal');
gui.add(conf, 'sizex',5,100);
gui.add(conf, 'sizey',5,100);
gui.add(conf, 'RELOAD');
gui.add(conf, 'FIND');
create();
}
function create(){
size = new Vec2(conf.sizex, conf.sizey);
grid = new Grid(size);
grid.createTable();
updateval = setInterval(function(){
grid.updateTable();
},10);
grid.setStart(new Vec2(conf.sizex-2,conf.sizey-2));
grid.setTarget(new Vec2(2,2));
startSELECTED = false;
targetSELECTED = false;
A = new Astar(grid);
}
Grid.prototype.createTable = function(){
var tablo = "";
for (var j = 0; j < this.size.y; j++) {
tablo += "<tr>";
for (var i = 0; i < this.size.x; i++) {
tablo += ("<td id='" + i + "x" + j + "' class='node'></td>");
}
tablo += "</tr>";
}
$("#tablo").html(tablo);
for (var i = 0; i < this.size.x; i++)
for (var j = 0; j < this.size.y; j++) {
var _this = this
$("#"+i + "x" + j).click(function(){
if(!startSELECTED){
startSELECTED=true;
grid.setStart(new Vec2(this.id.split("x")[0], this.id.split("x")[1]));
}else if(!targetSELECTED){
targetSELECTED=true;
grid.setTarget(new Vec2(parseInt(this.id.split("x")[0]), parseInt(this.id.split("x")[1])));
}else{
_this.nodes[this.id.split("x")[0]][this.id.split("x")[1]].blocked= true;
}
});
}
}
Grid.prototype.updateTable = function(){
for (var i = 0; i < this.size.x; i++)
for (var j = 0; j < this.size.y; j++) {
$("#"+i + "x" + j).css("background-color","#009999");
if(this.target.d(this.nodes[i][j].loc)==0)
$("#"+i + "x" + j).css("background-color","red");
else if(this.start.d(this.nodes[i][j].loc)==0)
$("#"+i + "x" + j).css("background-color","blue");
else{
if(this.nodes[i][j].onPath)
$("#"+i + "x" + j).css("background-color","grey");
else if(this.nodes[i][j].open)
$("#"+i + "x" + j).css("background-color","green");
else if(this.nodes[i][j].closed)
$("#"+i + "x" + j).css("background-color","orange");
else if(this.nodes[i][j].blocked)
$("#"+i + "x" + j).css("background-color","black");
}
if(this.nodes[i][j].gCost != 0 && this.nodes[i][j].gCost != 0){
var html = Math.floor(this.nodes[i][j].gCost);
html+="+"+Math.floor(this.nodes[i][j].hCost);
html+="</br>"+Math.floor(this.nodes[i][j].fCost());
$("#"+i + "x" + j).html(html);
}
}
}
Also see: Tab Triggers