jspsych-tangram 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3563,7 +3563,7 @@ function startConstructionTrial(display_element, params, _jsPsych) {
3563
3563
  return { root, display_element, jsPsych: _jsPsych };
3564
3564
  }
3565
3565
 
3566
- const info$1 = {
3566
+ const info$2 = {
3567
3567
  name: "tangram-construct",
3568
3568
  version: "1.0.0",
3569
3569
  parameters: {
@@ -3677,7 +3677,7 @@ class TangramConstructPlugin {
3677
3677
  this.jsPsych = jsPsych;
3678
3678
  }
3679
3679
  static {
3680
- this.info = info$1;
3680
+ this.info = info$2;
3681
3681
  }
3682
3682
  /**
3683
3683
  * Launches the trial by invoking startConstructionTrial
@@ -3821,7 +3821,7 @@ function startPrepTrial(display_element, params, jsPsych) {
3821
3821
  return { root, display_element, jsPsych };
3822
3822
  }
3823
3823
 
3824
- const info = {
3824
+ const info$1 = {
3825
3825
  name: "tangram-prep",
3826
3826
  version: "1.0.0",
3827
3827
  parameters: {
@@ -3897,7 +3897,7 @@ class TangramPrepPlugin {
3897
3897
  this.jsPsych = jsPsych;
3898
3898
  }
3899
3899
  static {
3900
- this.info = info;
3900
+ this.info = info$1;
3901
3901
  }
3902
3902
  /**
3903
3903
  * Launches the trial by invoking startPrepTrial
@@ -3933,6 +3933,329 @@ class TangramPrepPlugin {
3933
3933
  }
3934
3934
  }
3935
3935
 
3936
+ function startNBackTrial(display_element, params, _jsPsych) {
3937
+ const root = client.createRoot(display_element);
3938
+ root.render(React.createElement(NBackView, { params }));
3939
+ return { root, display_element, jsPsych: _jsPsych };
3940
+ }
3941
+ function NBackView({ params }) {
3942
+ const {
3943
+ tangram,
3944
+ isMatch,
3945
+ show_tangram_decomposition,
3946
+ instructions,
3947
+ button_text,
3948
+ duration,
3949
+ onTrialEnd
3950
+ } = params;
3951
+ const trialStartTime = React.useRef(Date.now());
3952
+ const buttonEnabledRef = React.useRef(true);
3953
+ const timeoutIdRef = React.useRef(null);
3954
+ const hasRespondedRef = React.useRef(false);
3955
+ const responseDataRef = React.useRef(null);
3956
+ const [buttonDisabled, setButtonDisabled] = React.useState(false);
3957
+ const CANON = /* @__PURE__ */ new Set([
3958
+ "square",
3959
+ "smalltriangle",
3960
+ "parallelogram",
3961
+ "medtriangle",
3962
+ "largetriangle"
3963
+ ]);
3964
+ const filteredTans = tangram.solutionTans.filter((tan) => {
3965
+ const tanName = tan.name ?? tan.kind;
3966
+ return CANON.has(tanName);
3967
+ });
3968
+ const mask = filteredTans.map((tan) => {
3969
+ const polygon = tan.vertices.map(([x, y]) => ({ x: x ?? 0, y: -(y ?? 0) }));
3970
+ return polygon;
3971
+ });
3972
+ const primitiveDecomposition = filteredTans.map((tan) => ({
3973
+ kind: tan.name ?? tan.kind,
3974
+ polygon: tan.vertices.map(([x, y]) => ({ x: x ?? 0, y: -(y ?? 0) }))
3975
+ }));
3976
+ const DISPLAY_SIZE = 400;
3977
+ const viewport = {
3978
+ w: DISPLAY_SIZE,
3979
+ h: DISPLAY_SIZE
3980
+ };
3981
+ const scaleS = React.useMemo(() => {
3982
+ const u = inferUnitFromPolys$1(mask);
3983
+ return u ? CONFIG.layout.grid.unitPx / u : 1;
3984
+ }, [mask]);
3985
+ const centerPos = {
3986
+ cx: viewport.w / 2,
3987
+ cy: viewport.h / 2
3988
+ };
3989
+ const pathD = (poly) => {
3990
+ if (!poly || poly.length === 0) return "";
3991
+ const moves = poly.map((p, i) => `${i === 0 ? "M" : "L"} ${p.x} ${p.y}`);
3992
+ return moves.join(" ") + " Z";
3993
+ };
3994
+ const endTrial = (data) => {
3995
+ if (timeoutIdRef.current) {
3996
+ clearTimeout(timeoutIdRef.current);
3997
+ timeoutIdRef.current = null;
3998
+ }
3999
+ const accuracy = isMatch !== void 0 ? isMatch === data.responded_match ? 1 : 0 : NaN;
4000
+ const trialData = {
4001
+ ...data,
4002
+ accuracy,
4003
+ tangram_id: tangram.tangramID,
4004
+ is_match: isMatch
4005
+ };
4006
+ if (onTrialEnd) {
4007
+ onTrialEnd(trialData);
4008
+ }
4009
+ };
4010
+ const handleButtonClick = () => {
4011
+ if (!buttonEnabledRef.current) {
4012
+ const rt_late = Date.now() - trialStartTime.current;
4013
+ hasRespondedRef.current = true;
4014
+ responseDataRef.current = {
4015
+ responded_match: true,
4016
+ rt: NaN,
4017
+ responded_after_duration: true,
4018
+ rt_after_duration: rt_late
4019
+ };
4020
+ endTrial(responseDataRef.current);
4021
+ } else {
4022
+ const rt = Date.now() - trialStartTime.current;
4023
+ buttonEnabledRef.current = false;
4024
+ setButtonDisabled(true);
4025
+ hasRespondedRef.current = true;
4026
+ responseDataRef.current = {
4027
+ responded_match: true,
4028
+ rt,
4029
+ responded_after_duration: false,
4030
+ rt_after_duration: NaN
4031
+ };
4032
+ }
4033
+ };
4034
+ React.useEffect(() => {
4035
+ timeoutIdRef.current = setTimeout(() => {
4036
+ buttonEnabledRef.current = false;
4037
+ if (hasRespondedRef.current && responseDataRef.current) {
4038
+ endTrial(responseDataRef.current);
4039
+ } else {
4040
+ endTrial({
4041
+ responded_match: false,
4042
+ rt: NaN,
4043
+ responded_after_duration: false,
4044
+ rt_after_duration: NaN
4045
+ });
4046
+ }
4047
+ }, duration);
4048
+ return () => {
4049
+ if (timeoutIdRef.current) {
4050
+ clearTimeout(timeoutIdRef.current);
4051
+ }
4052
+ };
4053
+ }, []);
4054
+ const renderSilhouette = () => {
4055
+ if (show_tangram_decomposition) {
4056
+ const rawPolys = primitiveDecomposition.map((primInfo) => primInfo.polygon);
4057
+ const placedPolys = placeSilhouetteGridAlignedAsPolys(rawPolys, scaleS, centerPos);
4058
+ return /* @__PURE__ */ React.createElement("g", { key: "sil-decomposed", pointerEvents: "none" }, placedPolys.map((scaledPoly, i) => /* @__PURE__ */ React.createElement(React.Fragment, { key: `prim-${i}` }, /* @__PURE__ */ React.createElement(
4059
+ "path",
4060
+ {
4061
+ d: pathD(scaledPoly),
4062
+ fill: CONFIG.color.silhouetteMask,
4063
+ opacity: CONFIG.opacity.silhouetteMask,
4064
+ stroke: "none"
4065
+ }
4066
+ ), /* @__PURE__ */ React.createElement(
4067
+ "path",
4068
+ {
4069
+ d: pathD(scaledPoly),
4070
+ fill: "none",
4071
+ stroke: CONFIG.color.tangramDecomposition.stroke,
4072
+ strokeWidth: CONFIG.size.stroke.tangramDecompositionPx
4073
+ }
4074
+ ))));
4075
+ } else {
4076
+ const placedPolys = placeSilhouetteGridAlignedAsPolys(mask, scaleS, centerPos);
4077
+ return /* @__PURE__ */ React.createElement("g", { key: "sil-unified", pointerEvents: "none" }, placedPolys.map((scaledPoly, i) => /* @__PURE__ */ React.createElement(
4078
+ "path",
4079
+ {
4080
+ key: `sil-${i}`,
4081
+ d: pathD(scaledPoly),
4082
+ fill: CONFIG.color.silhouetteMask,
4083
+ opacity: CONFIG.opacity.silhouetteMask,
4084
+ stroke: "none"
4085
+ }
4086
+ )));
4087
+ }
4088
+ };
4089
+ return /* @__PURE__ */ React.createElement("div", { style: {
4090
+ display: "flex",
4091
+ flexDirection: "column",
4092
+ alignItems: "center",
4093
+ justifyContent: "center",
4094
+ padding: "20px",
4095
+ background: "#f5f5f5"
4096
+ } }, instructions && /* @__PURE__ */ React.createElement(
4097
+ "div",
4098
+ {
4099
+ style: {
4100
+ maxWidth: "800px",
4101
+ width: "100%",
4102
+ marginBottom: "30px",
4103
+ textAlign: "center",
4104
+ fontSize: "18px",
4105
+ lineHeight: "1.5"
4106
+ },
4107
+ dangerouslySetInnerHTML: { __html: instructions }
4108
+ }
4109
+ ), /* @__PURE__ */ React.createElement("div", { style: {
4110
+ display: "flex",
4111
+ flexDirection: "column",
4112
+ alignItems: "center",
4113
+ gap: "30px"
4114
+ } }, /* @__PURE__ */ React.createElement(
4115
+ "svg",
4116
+ {
4117
+ width: viewport.w,
4118
+ height: viewport.h,
4119
+ viewBox: `0 0 ${viewport.w} ${viewport.h}`,
4120
+ style: {
4121
+ display: "block",
4122
+ background: CONFIG.color.bands.silhouette.fillEven,
4123
+ border: `${CONFIG.size.stroke.bandPx}px solid ${CONFIG.color.bands.silhouette.stroke}`,
4124
+ borderRadius: "8px"
4125
+ }
4126
+ },
4127
+ renderSilhouette()
4128
+ ), /* @__PURE__ */ React.createElement(
4129
+ "button",
4130
+ {
4131
+ className: "jspsych-btn",
4132
+ onClick: handleButtonClick,
4133
+ disabled: buttonDisabled,
4134
+ style: {
4135
+ padding: "12px 30px",
4136
+ fontSize: "16px",
4137
+ cursor: buttonDisabled ? "not-allowed" : "pointer",
4138
+ opacity: buttonDisabled ? 0.5 : 1
4139
+ }
4140
+ },
4141
+ button_text
4142
+ )));
4143
+ }
4144
+
4145
+ const info = {
4146
+ name: "tangram-nback",
4147
+ version: "1.0.0",
4148
+ parameters: {
4149
+ /** Single tangram specification to display */
4150
+ tangram: {
4151
+ type: jspsych.ParameterType.COMPLEX,
4152
+ default: void 0,
4153
+ description: "TangramSpec object defining target shape to display"
4154
+ },
4155
+ /** Whether this trial is a match (for computing accuracy) */
4156
+ isMatch: {
4157
+ type: jspsych.ParameterType.BOOL,
4158
+ default: void 0,
4159
+ description: "Whether this tangram matches the previous one (optional)"
4160
+ },
4161
+ /** Whether to show tangram decomposed into individual primitives with borders */
4162
+ show_tangram_decomposition: {
4163
+ type: jspsych.ParameterType.BOOL,
4164
+ default: false,
4165
+ description: "Whether to show tangram decomposed into individual primitives with borders"
4166
+ },
4167
+ /** HTML content to display above the tangram as instructions */
4168
+ instructions: {
4169
+ type: jspsych.ParameterType.STRING,
4170
+ default: "",
4171
+ description: "HTML content to display above the tangram as instructions"
4172
+ },
4173
+ /** Text to display on response button */
4174
+ button_text: {
4175
+ type: jspsych.ParameterType.STRING,
4176
+ default: "Same as previous!",
4177
+ description: "Text to display on response button"
4178
+ },
4179
+ /** Duration to display tangram and accept responses (milliseconds) */
4180
+ duration: {
4181
+ type: jspsych.ParameterType.INT,
4182
+ default: 3e3,
4183
+ description: "Duration in milliseconds to display tangram and accept responses"
4184
+ },
4185
+ /** Callback fired when trial ends */
4186
+ onTrialEnd: {
4187
+ type: jspsych.ParameterType.FUNCTION,
4188
+ default: void 0,
4189
+ description: "Callback when trial completes with full data"
4190
+ }
4191
+ },
4192
+ data: {
4193
+ /** Whether participant clicked the response button before duration expired */
4194
+ responded_match: {
4195
+ type: jspsych.ParameterType.BOOL,
4196
+ description: "True if participant clicked response button, false otherwise"
4197
+ },
4198
+ /** Reaction time in milliseconds (NaN if no response or response after duration) */
4199
+ rt: {
4200
+ type: jspsych.ParameterType.INT,
4201
+ description: "Milliseconds between trial start and button click (NaN if no response or late response)"
4202
+ },
4203
+ /** Accuracy: 1 if correct, 0 if incorrect, NaN if isMatch not provided */
4204
+ accuracy: {
4205
+ type: jspsych.ParameterType.FLOAT,
4206
+ description: "1 if response matches isMatch parameter, 0 otherwise (NaN if isMatch not provided)"
4207
+ },
4208
+ /** Whether response occurred after duration expired */
4209
+ responded_after_duration: {
4210
+ type: jspsych.ParameterType.BOOL,
4211
+ description: "True if button clicked after duration expired, false otherwise"
4212
+ },
4213
+ /** Time of late response (NaN if no late response) */
4214
+ rt_after_duration: {
4215
+ type: jspsych.ParameterType.INT,
4216
+ description: "Milliseconds between trial start and late button click (NaN if no late response)"
4217
+ }
4218
+ },
4219
+ citations: ""
4220
+ };
4221
+ class TangramNBackPlugin {
4222
+ constructor(jsPsych) {
4223
+ this.jsPsych = jsPsych;
4224
+ }
4225
+ static {
4226
+ this.info = info;
4227
+ }
4228
+ /**
4229
+ * Launches the trial by invoking startNBackTrial
4230
+ * with the display element, parameters, and jsPsych instance.
4231
+ */
4232
+ trial(display_element, trial) {
4233
+ const wrappedOnTrialEnd = (data) => {
4234
+ if (trial.onTrialEnd) {
4235
+ trial.onTrialEnd(data);
4236
+ }
4237
+ const reactContext = display_element.__reactContext;
4238
+ if (reactContext?.root) {
4239
+ reactContext.root.unmount();
4240
+ }
4241
+ display_element.innerHTML = "";
4242
+ this.jsPsych.finishTrial(data);
4243
+ };
4244
+ const params = {
4245
+ tangram: trial.tangram,
4246
+ isMatch: trial.isMatch,
4247
+ show_tangram_decomposition: trial.show_tangram_decomposition,
4248
+ instructions: trial.instructions,
4249
+ button_text: trial.button_text,
4250
+ duration: trial.duration,
4251
+ onTrialEnd: wrappedOnTrialEnd
4252
+ };
4253
+ const { root, display_element: element, jsPsych } = startNBackTrial(display_element, params, this.jsPsych);
4254
+ element.__reactContext = { root, jsPsych };
4255
+ }
4256
+ }
4257
+
3936
4258
  exports.TangramConstructPlugin = TangramConstructPlugin;
4259
+ exports.TangramNBackPlugin = TangramNBackPlugin;
3937
4260
  exports.TangramPrepPlugin = TangramPrepPlugin;
3938
4261
  //# sourceMappingURL=index.cjs.map