ImageGrid/ui/experiments/editor.html

262 lines
5.4 KiB
HTML
Raw Normal View History

<html>
<style>
button,
details {
border: solid 1px gray;
border-radius: 4px;
margin: 1px;
background: white;
}
details {
width: 500px;
}
details > div {
margin: 10px;
}
summary {
padding-left: 3px;
background: #ddd;
}
input[type=range] {
width: 300px;
}
/* XXX this is not live...
input[type=range]:after {
content: attr(value);
color: black;
}
*/
div > span:first-child {
display: inline-block;
width: 100px;
}
/* range syling... */
input[type='range'] {
-webkit-appearance: none !important;
background: silver;
height: 4px;
border: solid 1px gray;
border-radius: 2px;
}
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none !important;
background: white;
height: 15px;
width: 15px;
border: solid 1px gray;
border-radius: 50%;
}
.value {
-webkit-appearance: none !important;
display: inline-block;
width: 40px;
text-align: right;
margin-left: 5px;
margin-right: 5px;
background: white;
border: none;
border-radius: 2px;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none !important;
/*
display: inline-block;
background: white;
border-radius: 2px;
border: solid 1px gray;
width: 10px;
*/
}
</style>
<script src="jquery.js"></script>
<script>
function updateFilter(e, f, v){
e = $(e)
var state = e
.css('-webkit-filter')
state = state == 'none' ? '' : state + ' '
//state = state.replace(RegExp(f+'\\s*\\([^\\)]*\\)'), '')
//state += f+'('+v+')'
if(RegExp(f).test(state)){
state = state.replace(RegExp(f+'\\s*\\([^\\)]*\\)'), f+'('+v+')')
} else {
state += f+'('+v+')'
}
e.css({
'-webkit-filter': state,
'filter': state
})
return v
}
function resetFilter(e, f){
e = $(e)
var state = e
.css('-webkit-filter')
state = state == 'none' ? '' : state + ' '
state = state.replace(RegExp(f+'\\s*\\([^\\)]*\\)'), '').trim()
e.css({
'-webkit-filter': state,
'filter': state
})
return e
}
function resetPrevRange(e){
e = $(e).prev('input[type=range]')
e.val(e.attr('default'))
.change()
}
// XXX this does not set hue correctly...
function loadSliderState(){
var state = $('#image')
.css('-webkit-filter')
.split(/\s*\(\s*|\s*\)\s*/g)
.reverse()
.slice(1)
// reset sliders do defaults...
$('input[type=range]').each(function(i, e){
e = $(e)
e.val(e.attr('default'))
})
// set the saved values...
while(state.length > 0){
var e = $('#'+state.pop())
if(e.prop('normalize')){
e.val(v2r(parseFloat(state.pop())))
} else {
e.val(parseFloat(state.pop()))
}
}
}
function saveState(){
var l = $('.state').length
var state = $('#image').css('-webkit-filter')
$('<button></button>')
.text(l)
.addClass('state '+l)
.attr('state', state)
.click(function(){
$('#image').css('-webkit-filter', state)
loadSliderState()
})
.appendTo($('.states'))
}
function clearStates(){
$('.state').remove()
}
function makeAbsRange(text, filter, target, min, max, dfl, step, tran, normalize){
tran = tran == null ? function(v){return v} : tran
normalize = normalize == null ? false : true
var elem = $('<div class="range"></div>')
$('<span class="title"/>')
.html(text)
.appendTo(elem)
var range = $('<input type="range">')
.attr({
id: filter,
min: min,
max: max,
step: step,
default: dfl,
})
.prop('normalize', normalize)
.val(dfl)
.change(function(){
var val = this.valueAsNumber
value.val(val)
updateFilter(target, this.id, tran(val))
})
.appendTo(elem)
var value = $('<input type="number" class="value"/>')
.attr({
min: min,
max: max,
step: step,
})
.val(dfl)
.change(function(){
range.val($(this).val())
})
.appendTo(elem)
$('<button class="reset">x</button>')
.click(function(){
range.val(dfl).change()
resetFilter(target, filter)
})
.appendTo(elem)
return elem
}
function makeLogRange(text, filter, target){
return makeAbsRange(text, filter, target, -100, 100, 0, 0.1, r2v, true)
}
var C = 43.47
function r2v(r){
return Math.pow(Math.E, r/C)
}
function v2r(v){
return Math.log(v)*C
}
$(function(){
$('.controls')
.append(makeLogRange('Brightness:', 'brightness', '#image'))
.append(makeLogRange('Contrast:', 'contrast', '#image'))
.append(makeLogRange('Saturation:', 'saturate', '#image'))
.append(makeAbsRange('Hue:', 'hue-rotate', '#image', -180, 180, 0, 0.5, function(v){ return v+'deg' }))
.append($('<hr>'))
.append(makeAbsRange('Grayscale:', 'grayscale', '#image', 0, 1, 0, 0.01))
.append($('<hr>'))
.append(makeAbsRange('Invert:', 'invert', '#image', 0, 1, 0, 0.01))
.append(makeAbsRange('Sepia:', 'sepia', '#image', 0, 1, 0, 0.01))
.append($('<hr>'))
.append($('<button onclick="$(\'.reset\').click()">Reset all</button>'))
.append($('<hr>'))
.append($('<div>NOTE: at this point order of operations is '+
'sometimes segnificant -- might be good to treat the filters '+
'as layers -- se Adobe CSS filter factory </div>'))
saveState()
})
</script>
<body>
<img id="image" src="image.jpg">
<details open>
<summary>Controls</summary>
<div class="controls">
</div>
</details>
<details open>
<summary>Snapshots</summary>
<div>
<div class="states">
</div>
<button onclick="saveState()">Save</button>
<button onclick="clearStates()">Clear</button>
</div>
</details>
</body>
</html>