From 76af6015f1135ecf923b02e5a89e0c9633c4dfbc Mon Sep 17 00:00:00 2001 From: Neil Graham Date: Thu, 11 Jan 2024 08:00:33 +1300 Subject: [PATCH] Brush opacity --- web/page/bspaint.js | 31 ++++++++++++++++++++++++++++--- web/page/index.html | 6 ++++++ web/page/style.css | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/web/page/bspaint.js b/web/page/bspaint.js index ee68390..3f1f61d 100644 --- a/web/page/bspaint.js +++ b/web/page/bspaint.js @@ -363,7 +363,7 @@ function createDrawArea(canvas = blankCanvas(),initialTitle="Image") { if (!layer.visible) continue; canvas.ctx.globalAlpha = layer.opacity; canvas.ctx.globalCompositeOperation=layer.compositeOperation; - if (this.isDrawing && layer===this.activeLayer) { + if (this.isDrawing && layer===this.activeLayer) { canvas.ctx.drawImage(activeOperationCanvas,0,0); } else { layer.draw(canvas.ctx); @@ -456,11 +456,15 @@ function createDrawArea(canvas = blankCanvas(),initialTitle="Image") { ctx.restore(); const unitRange=this.strokeCoordinates.map(({x,y})=>({x:x/canvas.width,y:y/canvas.height})); + ctx.save(); + const opacity = $("#brush_opacity input[type=range]").val()/100; + ctx.globalAlpha=opacity; const strokes = this.strokeModifier([unitRange]) for (const unitStroke of strokes) { const stroke=unitStroke.map(({x,y})=>({x:x*canvas.width,y:y*canvas.height})); tip.tool.drawOperation(activeOperationCanvas.ctx,tip,stroke) } + ctx.restore(); if (this.activeLayer.mask) { const ctx=activeOperationCanvas.ctx; ctx.save(); @@ -830,7 +834,8 @@ function initPaint(){ }); - + $("input.percentage").on("change",processPercentageInput); + $("input.percentage~input").on("input",processPercentageRange); window.test1=createDrawArea(undefined,"Image A"); window.test2=createDrawArea(undefined,"Image B"); test1.setPosition(30,60) ; @@ -1507,8 +1512,28 @@ document.addEventListener('paste', async (event) => { } }); +function processPercentageInput(e) { + let element = e.currentTarget; + if (!element.hasOwnProperty("lastGoodValue")) element.lastGoodValue=100; + let range = element.parentElement.querySelector("input[type=range]"); + let value = parseFloat(element.value); + if (Number.isNaN(value)) { + value=element.lastGoodValue; + } + value=Math.floor(value); + if (range) { + range.value=value; + value=range.value; + } + element.value=`${value}%`; +} - +function processPercentageRange(e) { + const range = e.currentTarget; + console.log(range.value); + const input = range.parentNode.querySelector(".percentage") + input.value = ""+range.value+"%"; +} hotkeys["CTRL_Z"] = _=>{activePic?.undo()} hotkeys["CTRL_SHIFT_Z"] = _=>{activePic?.redo()} diff --git a/web/page/index.html b/web/page/index.html index 86ee6a6..e3bc663 100644 --- a/web/page/index.html +++ b/web/page/index.html @@ -39,6 +39,12 @@
+
+ + +
+ +
diff --git a/web/page/style.css b/web/page/style.css index 630f730..27903a5 100644 --- a/web/page/style.css +++ b/web/page/style.css @@ -252,6 +252,24 @@ input[type="color"] { border:none; outline:none; } + +#brush_opacity { + width:100%; + padding:0; + margin:0; + position:relative; +} +#brush_opacity input { + width:100%; + box-sizing:border-box; +} +#brush_opacity label{ + font-size:small; +} +#brush_opacity output{ + font-size:small; +} + #foreground { position: absolute; bottom : 32px;