Browse Source

CTRLPainter now handles scaling from the UI interface. Also added ability (and UI button) to fit-to-canvas

dev
Bryan Miller 5 years ago
parent
commit
46ed0fddd4
2 changed files with 45 additions and 10 deletions
  1. +41
    -10
      app/js/ctrls/CTRLPainter.js
  2. +4
    -0
      views/ctrlpainter.html

+ 41
- 10
app/js/ctrls/CTRLPainter.js View File

input.enableMouseInput(true); input.enableMouseInput(true);
input.preventDefaults = true; input.preventDefaults = true;


// Mouse handling...
/*input.listen("mousemove", handle_mouseevent);
input.listen("mousedown", handle_mouseevent);
input.listen("mouseup", handle_mouseevent);
input.listen("mouseclick", handle_mouseclickevent);
*/



/* -------------------------------------------------------------------- /* --------------------------------------------------------------------
* CTRLPainter * CTRLPainter
} else if (this.__palette !== null && this.__surface.palette !== this.__palette){ } else if (this.__palette !== null && this.__surface.palette !== this.__palette){
this.__surface.palette = this.__palette; this.__surface.palette = this.__palette;
} }
this.scale_to_fit();
this.center_surface(); this.center_surface();
} }
RenderD(); RenderD();
}).bind(this); }).bind(this);
input.listen("wheel", handle_scale); input.listen("wheel", handle_scale);



var elscale = document.querySelector("#painter_scale");
if (elscale){
var self = this;
elscale.addEventListener("change", function(e){
var val = Number(this.value);
self.scale = val;
RenderD();
});
elscale.value = this.__scale;
}

var handle_fittocanvas = (function(){
this.scale_to_fit();
this.center_surface();
RenderD();
}).bind(this);
GlobalEvents.listen("painter-fit-to-canvas", handle_fittocanvas);

var handle_togglegrid = (function(target, args){ var handle_togglegrid = (function(target, args){
if (args.show){ if (args.show){
target.classList.add("pure-button-active"); target.classList.add("pure-button-active");
if (typeof(s) !== 'number') if (typeof(s) !== 'number')
throw new TypeError("Expected number value."); throw new TypeError("Expected number value.");
this.__scale = Math.max(0.1, Math.min(100.0, s)); this.__scale = Math.max(0.1, Math.min(100.0, s));
var elscale = document.querySelector("#painter_scale");
if (elscale){
elscale.value = this.__scale;
}
} }


get showGrid(){return this.__gridEnabled;} get showGrid(){return this.__gridEnabled;}
context = canvas.getContext("2d"); context = canvas.getContext("2d");
if (!context) if (!context)
throw new Error("Failed to obtain canvas context."); throw new Error("Failed to obtain canvas context.");
context.imageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize". ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
input.mouseTargetElement = canvas; input.mouseTargetElement = canvas;


this.scale_to_fit();
this.center_surface(); this.center_surface();
} }
return this; return this;
if (canvas === null || this.__surface === null) if (canvas === null || this.__surface === null)
return; return;


this.__offset[0] = Math.floor((canvas.clientWidth - this.__surface.width) * 0.5);
this.__offset[1] = Math.floor((canvas.clientHeight - this.__surface.height) * 0.5);
this.__offset[0] = Math.floor((canvas.clientWidth - (this.__surface.width * this.__scale)) * 0.5);
this.__offset[1] = Math.floor((canvas.clientHeight - (this.__surface.height * this.__scale)) * 0.5);
return this; return this;
} }


scale_to_fit(){
if (canvas === null || this.__surface === null)
return;

var sw = canvas.clientWidth / this.__surface.width;
var sh = canvas.clientHeight / this.__surface.height;
var s = Math.min(sw, sh).toString();
var di = s.indexOf(".");
if (di >= 0)
s = s.slice(0, di+2);
this.scale = Number(s);
}

render(){ render(){
if (context === null || this.__surface === null) if (context === null || this.__surface === null)
return; return;

+ 4
- 0
views/ctrlpainter.html View File

Scale : Scale :
</label> </label>
<input id="painter_scale" class="input-u-1-6" type="number" min="0.5" max="1000" step="0.1" value ="1"/> <input id="painter_scale" class="input-u-1-6" type="number" min="0.5" max="1000" step="0.1" value ="1"/>

<button emit="painter-fit-to-canvas" class="pure-button">
<i class="fa fa-expand-arrows-alt"></i>
</button>
</div> </div>


<div class="pure-u-1-3 simple-padding"> <div class="pure-u-1-3 simple-padding">

Loading…
Cancel
Save