我正在使用Pixastic 库添加一些图像编辑工具。这个想法是用户可以从选择框中选择他们想要的图像或工具的一个方面,然后该工具将显示在选择框下方(我正在使用select2)并且用户可以通过滑块进行编辑。这是我到目前为止所拥有的:
# This seeds the select2 options list
imageToolsList = [
{id: 'bl', text: 'Blur'}
{id: 'bc', text: 'Brightness/Contrast'}
{id: 'ca', text: 'Color Adjust (RGB)'}
...
]
#Creates a select box and calls imageTooler function when the value of the box is changed
$(".image_tools_select").each ->
$(@).select2
placeholder: "Select an adjustment tool."
data: imageToolsList
$(@).on("change", (i) ->
imageTooler JSON.stringify(
val: i.val
clipName: $(@).closest('.clip').attr('id')
)
)
# The function called on the value that the select box is changed to
imageTooler = (i) ->
imageData = jQuery.parseJSON(i)
iId = imageData.val
imageClipName = imageData.clipName
newTool = "<div id=#{iId}><label>#{iId}</label><div class='slider#{iId}'></div></div>"
$("##{imageClipName}").find(".imagetoolfields").append newTool
This succeeds in appending the name of the editing tool and the correct slider div beneath the select box when a tool is chosen, but what I'd really like is dynamically create a slider function for that particular tool and image (there are multiple images on一个页面,每个页面都有自己的编辑工具带)。这是一个适用于“模糊”工具的滑块功能:
$('.sliderbl').slider
min: 0
max: 5
value: 0.5
step: 0.1
range: "min"
slide: (event, ui) ->
$("#img_snapshot_16").pixastic("blurfast", {amount:ui.value})
有没有办法扩展 imageToolsList 使它看起来像:
imageToolsList = [
{id: 'bl', text: 'Blur', tool: $("##{imageClipName}").pixastic("blurfast", {amount:ui.value}), sliderVals: {min: 0, max: 5, value: 0.5, step: 0.1, range: "min"} }
...
]
然后为 中的每个工具动态创建 jQuery 滑块函数imageTooler
,就像 div 和滑块 div 所做的那样?