<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>local state save</title>
<meta name="description" content="local state save">
<script src="https://aframe.io/releases/0.4.0/aframe.min.js"></script>
</head>
<script type="text/javascript">
// added to https://github.com/Utopiah/aframe-persist-component
AFRAME.registerSystem('persist', {
schema: {
savekey: {
type: 'int',
default: 13 // 13 = enter, cf https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#Value_of_keyCode
},
},
init: function () {
this.entities = [];
window.addEventListener("unload", saveAll);
if (this.data.savekey) document.addEventListener("keydown", keyDown, false);
var entities = this.entities;
var savekey = this.data.savekey;
function saveAll(){
for (var i = 0; i < entities.length; i++) {
document.getElementById(entities[i].id).components[entities[i].attrName].saveValue();
}
}
function forgetAll(){
for (var i = 0; i < entities.length; i++) {
document.getElementById(entities[i].id).components[entities[i].attrName].forgetValue();
}
}
function keyDown(e) {
var keyCode = e.keyCode;
if(keyCode==savekey) {
console.log("Manual save");
saveAll();
}
}
},
registerMe: function (el) {
this.entities.push(el);
}
});
AFRAME.registerComponent('persist', {
multiple: true,
// arguably could have been better to accept a list of attributes to save
schema: {
attribute: {
type: 'string',
default: 'position'
},
debug: {
type: 'boolean',
default: false
}
},
init: function () {
if (!this.el.id){
console.log("error", this.attrName, "requires an element id on your entity");
return;
}
this.data.storageName = this.attrName+'_'+this.el.id+'_'+this.data.attribute;
this.loadValue();
if (this.data.debug) console.log(this.data.storageName);
this.system.registerMe({attrName: this.attrName, id: this.el.id})
},
loadValue: function(){
let savedValue = localStorage.getItem(this.data.storageName);
if (savedValue){
if (this.data.debug) console.log('Previous value of', this.data.attribute, 'for', savedValue,
'saved from', this.data.storageName, ', using it');
this.el.setAttribute(this.data.attribute, savedValue);
}
},
saveValue: function(){
let attributeValue = this.el.getAttribute(this.data.attribute);
if ((this.data.attribute == "position") || (this.data.attribute == "rotation"))
localStorage.setItem(this.data.storageName, AFRAME.utils.coordinates.stringify(attributeValue));
else
localStorage.setItem(this.data.storageName, attributeValue);
if (this.data.debug) console.log(this.data.storageName, 'saved', this.data.attribute, 'of value', attributeValue);
},
forgetValue: function(){
if (this.data.debug) console.log(this.data.storageName, 'deleted');
localStorage.removeItem(this.data.storageName);
}
});
</script>
<body>
<a-scene>
<a-camera id="testingid" persist="debug:true;"
persist__rot="debug:true; attribute:rotation;"></a-camera>
<a-box id="testingid2" visible="false"
persist__pos="debug:true; attribute:position;"
persist__vis="debug:true; attribute:visible;"
persist__rot="debug:true; attribute:rotation;"></a-box>
<!-- commented out to remove shader errors...
<a-box scale="20 20 20" color="grey" scale="1" material="side:double"></a-box>
-->
</a-scene>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |