I'm trying to incorporate an opacity slider so that any selected objects are set to change based on the slider's position (100 being completely visible). I'm using Fabric.js version 1.7.22 and jQuery 3.3.1.
I originally asked this and got a working solution (thank you @Durga) but realized I was using the wrong Fabric.js version. I've looked over the docs but am stuck. What am I doing wrong here?
What I have: http://jsfiddle.net/code4ever/srm25f9d/
var canvas = new fabric.Canvas("c");
canvas.isDrawingMode = true;
// select, draw
$("#select").click(function() {
canvas.isDrawingMode = false;
});
$("#draw").click(function() {
canvas.isDrawingMode = true;
});
var activeObject = null;
canvas.on('selection:created', function(options) {
activeObject = options.target;
$("#alpha").slider("option", "value", activeObject.opacity);
});
canvas.on('selection:updated', function(options) {
activeObject = options.target;
$("#alpha").slider("option", "value", activeObject.opacity);
});
canvas.on('selection:cleared', function(options) {
activeObject = null;
});
$("#alpha").slider({
max: 1,
min: 0,
step: 0.1,
value: 1,
slide: function(event, ui) {
activeObject && (activeObject.opacity = ui.value)
canvas.requestRenderAll();
},
stop: function(event, ui) {
canvas.requestRenderAll();
}
});
canvas {
border: solid 1px #000;
}
fieldset {
max-width: 350px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id="c" width="400" height="400"></canvas>
<br>
<button id="draw">Draw</button>
<button id="select">Select</button>
<br>
<br>
<fieldset>
<legend>Controls</legend>
<label for="alpha">Opactity</label>
<div id="alpha" name="alpha"></div>
</fieldset>
Thanks in advance!