Added cursor drag

This commit is contained in:
Devine Lu Linvega 2017-09-28 14:48:38 +13:00
parent 10be02f444
commit 83b7d32062
11 changed files with 59 additions and 42 deletions

View File

@ -70,6 +70,9 @@ Missing documentation.
Missing documentation. Missing documentation.
### Settings ### Settings
- `thickness`, default 30
- `color`, default black
- `cap`, default square
### Methods ### Methods
- `stroke:`, no details. - `stroke:`, no details.
@ -82,11 +85,12 @@ Missing documentation.
Cursor magnetisation settings. Cursor magnetisation settings.
### Settings ### Settings
- `size`, default 15 - `size`, default 0
- `rate`, default 4 - `step`, default 4
### Methods ### Methods
- `lock:`, no details. - `lock:`, no details.
- `unlock:`, no details.
### Ports ### Ports

View File

@ -3,7 +3,6 @@
<script type="text/javascript" src="scripts/core/module.js"></script> <script type="text/javascript" src="scripts/core/module.js"></script>
<script type="text/javascript" src="scripts/modules/frame.js"></script> <script type="text/javascript" src="scripts/modules/frame.js"></script>
<script type="text/javascript" src="scripts/modules/brush.js"></script> <script type="text/javascript" src="scripts/modules/brush.js"></script>
<script type="text/javascript" src="scripts/modules/eraser.js"></script>
<script type="text/javascript" src="scripts/modules/line.js"></script> <script type="text/javascript" src="scripts/modules/line.js"></script>
<script type="text/javascript" src="scripts/modules/io.js"></script> <script type="text/javascript" src="scripts/modules/io.js"></script>
<script type="text/javascript" src="scripts/modules/path.js"></script> <script type="text/javascript" src="scripts/modules/path.js"></script>

View File

@ -61,7 +61,10 @@ function Cursor(rune)
} }
else if(e.altKey){ else if(e.altKey){
ronin.eraser.stroke(ronin.cursor.line); ronin.brush.erase(ronin.cursor.line);
}
else if(e.shiftKey){
ronin.cursor.drag(ronin.cursor.line);
} }
else{ else{
ronin.brush.stroke(ronin.cursor.line); ronin.brush.stroke(ronin.cursor.line);
@ -91,6 +94,20 @@ function Cursor(rune)
ronin.cursor.query = ronin.commander.input_el.value; ronin.cursor.query = ronin.commander.input_el.value;
} }
this.drag = function(line)
{
var offset = make_offset(line.from,line.to);
var data = ronin.render.select();
ronin.render.clear();
ronin.render.context().putImageData(data, offset.x * -2, offset.y * -2);
}
function make_offset(a,b)
{
return {x:a.x-b.x,y:a.y-b.y};
}
this.inject_query = function() this.inject_query = function()
{ {
if(ronin.cursor.query && ronin.cursor.query.indexOf("$") < 0){ return; } if(ronin.cursor.query && ronin.cursor.query.indexOf("$") < 0){ return; }

View File

@ -28,12 +28,12 @@ function Layer()
this.el.style.height = size.height+"px"; this.el.style.height = size.height+"px";
} }
this.select = function(x,y,width,height) this.select = function(x = 0,y = 0,width = ronin.frame.settings.width,height = ronin.frame.settings.width)
{ {
return this.context().getImageData(x, y, width * 2, height * 2); return this.context().getImageData(x, y, width * 2, height * 2);
} }
this.to_data = function() this.to_base64 = function()
{ {
return this.el.toDataURL('image/png'); return this.el.toDataURL('image/png');
} }
@ -41,7 +41,7 @@ function Layer()
this.to_img = function() this.to_img = function()
{ {
var img = new Image(); var img = new Image();
img.src = this.to_data(); img.src = this.to_base64();
return img; return img;
} }

View File

@ -6,6 +6,8 @@ function Grid()
this.draw = function(size = 60, step = 5) this.draw = function(size = 60, step = 5)
{ {
this.clear();
var x = 1; var x = 1;
var size = size * 2; var size = size * 2;
while(x < this.el.width/size){ while(x < this.el.width/size){

View File

@ -83,6 +83,20 @@ function Brush()
} }
} }
this.erase = function(line)
{
var ctx = ronin.render.context();
ctx.beginPath();
ctx.globalCompositeOperation="destination-out";
ctx.moveTo(line.from.x * 2,line.from.y * 2);
ctx.lineTo(line.to.x * 2,line.to.y * 2);
ctx.lineCap="round";
ctx.lineWidth = this.thickness(line);
ctx.stroke();
ctx.closePath();
}
this.mod_size = function(mod) this.mod_size = function(mod)
{ {
this.settings.size = clamp(this.settings.size+mod,1,100); this.settings.size = clamp(this.settings.size+mod,1,100);

View File

@ -1,23 +0,0 @@
function Eraser()
{
Module.call(this,"eraser");
this.thickness = function(line)
{
return ronin.brush.thickness(line);
}
this.stroke = function(line)
{
var ctx = ronin.render.context();
ctx.beginPath();
ctx.globalCompositeOperation="destination-out";
ctx.moveTo(line.from.x * 2,line.from.y * 2);
ctx.lineTo(line.to.x * 2,line.to.y * 2);
ctx.lineCap="round";
ctx.lineWidth = this.thickness(line);
ctx.stroke();
ctx.closePath();
}
}

View File

@ -37,7 +37,7 @@ function IO()
this.render = function() this.render = function()
{ {
var fs = require('fs'); var fs = require('fs');
var data = ronin.render.to_data().replace(/^data:image\/\w+;base64,/, ""); var data = ronin.render.to_base64().replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64'); var buf = new Buffer(data, 'base64');
dialog.showSaveDialog((fileName) => { dialog.showSaveDialog((fileName) => {

View File

@ -2,26 +2,29 @@ function Magnet()
{ {
Module.call(this,"magnet","Cursor magnetisation settings."); Module.call(this,"magnet","Cursor magnetisation settings.");
this.settings = {size:15,rate:4}; this.settings = {size:0,step:4};
this.methods.lock = function(q) this.methods.lock = function(q)
{ {
var size = parseInt(q); var size = parseInt(q);
if(size < 5){ return; } if(size < 5){ this.unlock(); return; }
console.log(size) ronin.grid.draw(size,ronin.magnet.settings.step);
ronin.grid.draw(size);
} }
this.methods.unlock = function(q) this.methods.unlock = function(q)
{ {
console.log(q) ronin.magnet.settings.size = 0;
ronin.grid.clear();
} }
this.filter = function(pos) this.filter = function(pos)
{ {
if(this.settings.size < 5){
return pos;
}
var s = this.settings.size; var s = this.settings.size;
return {x:parseInt(pos.x/s)*s,y:parseInt(pos.y/s)*s}; return {x:parseInt(pos.x/s)*s,y:parseInt(pos.y/s)*s};
} }

View File

@ -2,6 +2,8 @@ function Path()
{ {
Module.call(this,"path"); Module.call(this,"path");
this.settings = {thickness:30,color:"black",cap:"square"};
this.methods.stroke = function(q) this.methods.stroke = function(q)
{ {
ronin.preview.clear(); ronin.preview.clear();
@ -32,9 +34,9 @@ function Path()
var ctx = ronin.preview.context(); var ctx = ronin.preview.context();
ctx.beginPath(); ctx.beginPath();
ctx.lineCap = "butt"; ctx.lineCap = q.settings.cap ? q.settings.cap : ronin.path.settings.cap;
ctx.lineWidth = 30; ctx.lineWidth = q.settings.thickness ? q.settings.thickness : ronin.path.settings.thickness;
ctx.strokeStyle = "black"; ctx.strokeStyle = q.settings.color ? q.settings.color : ronin.path.settings.color;
ctx.stroke(new Path2D(path)); ctx.stroke(new Path2D(path));
ctx.closePath(); ctx.closePath();
} }

View File

@ -16,7 +16,6 @@ function Ronin()
this.io = new IO(); this.io = new IO();
this.brush = new Brush(); this.brush = new Brush();
this.eraser = new Eraser();
this.frame = new Frame(); this.frame = new Frame();
this.line = new Line(); this.line = new Line();
this.path = new Path(); this.path = new Path();
@ -77,6 +76,6 @@ function Ronin()
// this.commander.input_el.value = "io import:~/Desktop/test.png anchor=$"; // this.commander.input_el.value = "io import:~/Desktop/test.png anchor=$";
// this.commander.input_el.value = "path stroke:$+"; // this.commander.input_el.value = "path stroke:$+";
this.commander.input_el.value = "magnet lock:"; // this.commander.input_el.value = "magnet lock:";
} }
} }